1

I am querying data from database using 'JdbcTemplate' spring bean. The returned object contains a data field. The problem I am facing is when I console print returnedObject.getDate().toString() it returns "2017-08-04", but when I do new Date().toString() then it returns Mon Mar 25 17:36:32 GMT 2019. I have read that java Date class does not contain any format in it and to format any Date we use SimpleDateFormat class. So my question is what parameter in Date object is causing it to print in different formats and how do I change its behaviour ?

rainyday
  • 353
  • 3
  • 17
  • Check the `toString()` method implemented by class of object returned by `returnedObject.getDate()` Most likely, `java.util.Date` is not same as class returned by `returnedObject.getDate()` hence both are returning two different formats. – Pushpesh Kumar Rajwanshi Mar 25 '19 at 18:06
  • @PushpeshKumarRajwanshi it is a java.util Date field – rainyday Mar 25 '19 at 18:09
  • 1
    Class return by `returnedObject.getDate()` should be `java.sql.Date` where as `new Date()` class is `java.util.Date` and as both are different hence the `toString()` for both will be different. – Pushpesh Kumar Rajwanshi Mar 25 '19 at 18:15
  • I recommend you don’t use `Date`. That class is poorly designed and long outdated (and the `java.sql.Date` correctly mentioned in some answers makes it even worse). Instead use `LocalDate` from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. Mar 26 '19 at 07:42

4 Answers4

2

JDBCTemplate is returning a java.sql.Date instance which extends java.util.Date. The JavaDocs of that toString() implementation explain the used date format:

Formats a date in the date escape format yyyy-mm-dd.

The toString() implementation of java.util.Date uses a longer date-time format:

Converts this Date object to a String of the form: dow mon dd hh:mm:ss zzz yyyy

The type of your date field could be Object and you could still assign a java.sql.Date or any subclass to the field. It's the runtime class of the variable that determines the behavior through dynamic method dispatch, i.e. which toString() implementation gets invoked.

If you want to convert the subclass to a "plain" java.util.Date instance, you can look here. Note that the time part will always be omitted from an SQL DATE. If you need that information, you should use TIMESTAMP instead.

Mick Mnemonic
  • 7,808
  • 2
  • 26
  • 30
  • that field variable's type is indeed java.util.Date and it gets set in my `customObjects` builder by fetching date from `ResultSet` from custom `ResultSetExtractor`. – rainyday Mar 25 '19 at 18:27
  • So, did this answer your question or do you have further concerns? – Mick Mnemonic Mar 25 '19 at 18:28
  • no, I am still not convinced why that field of type `java.util.Date` prints in 'yyyy-mm-dd' format. – rainyday Mar 25 '19 at 18:31
  • Because that's how inheritance works. The type of the field could be `Object` and you could still assign a `java.sql.Date` or any subclass to the field. It's the _runtime class_ of the variable that determines the behavior through dynamic method dispatch. – Mick Mnemonic Mar 25 '19 at 18:35
  • perfect ! if you add above comment in your answer I will accept your answer. Sadly I wasted half of the day for such a small thing, I should have thought a little more. – rainyday Mar 25 '19 at 18:57
1

when I console print returnedObject.getDate().toString() it returns "2017-08-04", but when I do new Date().toString() then it returns Mon Mar 25 17:36:32 GMT 2019

That's simple.

The returnedObject.getDate() code returns an object of java.sql.Date type whereas most likely the Date class in your second case is java.util.Date.

As per the java docs of java.sql.Date, its toString method returns a String in yyyy-mm-dd format.

And as per the java docs of java.util.Date, its toString method returns a String in dow mon dd hh:mm:ss zzz yyyy format.

VHS
  • 9,534
  • 3
  • 19
  • 43
0

Although the Date class is intended to reflect coordinated universal time (UTC), it may not do so exactly, depending on the host environment of the Java Virtual Machine, according to the Java documentation of the date class. I think that means that it is looking at the your system's date format while the SQL query comes with the UTC format. There is not date parameter that changes the format. You need to apply it like you said via the date formater.Look at the DateFormater class: https://docs.oracle.com/javase/7/docs/api/java/text/DateFormat.html Particularly the format method.

JustAFellowCoder
  • 300
  • 2
  • 11
0

java.sql.Date extends java.util.Date. You can get both sorts of dates in ms since 1970 using getTime() and setTime(long time) can be used to create identical types, so you can get the string in whatever format you like.

However, note that Java's old date classes are garbage (not thread safe, not immutable, and poorly named (they are timestamps not dates).

There are 3rd party libraries that are superior. Look at java.util.time which is the new way to do it.

ggb667
  • 1,881
  • 2
  • 20
  • 44