0

I collect Date from my Spring MVC/ Hibernate app using the following code:

String startMembershipDate = request.getParameter("startmembershipdate");

and I am using the following field definition for this field in my entity

@Column(name="MEMBERSHIP_DATE", nullable=false, unique=false)
private Date membershipDate;

So I was wondering if someone can please provide me with an example on how to convert the received date in the JSP to GMT format using Joda-time?

Also shall I change the field definition from Date to DateTime? Timestamp?

MChan
  • 6,842
  • 27
  • 83
  • 132

1 Answers1

2

See this question how to convert between Date and DateTime: Convert from java.util.date to JodaTime

This question tells you how to get format the DateTime using GMT: JodaTime DateTime, ISO8601 GMT date format

You can change the field definition to DateTime if you need to. You just have to write a custom Hibernate mapper then. We do that here and it works well (especially since we save dates as DECIMAL(16) so we don't get stupid bugs because of odd SQL DATE implementations, driver bugs, ...).

java.sql.Timestamp is something else. It's derived from java.util.Date and only adds the notion of nano seconds - which introduces some new subtleties that you need to be aware of. In my code, I stay away from it.

But unless you have a good reason (like finding that you convert to DateTime all the time), leave it alone.

Community
  • 1
  • 1
Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820
  • My reason is that I have an app that have users from different locations around the world each with their own timezone and time format, so after reading a lot of SO articles related to this matter I noted that everyone recommends unifying the date format saved on server to GMT format then formatting the view for each user depending on his/her local date format...Please correct me if I am following wrong approach here? I am not sure how to persist GMT in DB using Date but read that it is super easy with Joda – MChan Jan 23 '14 at 13:15
  • Ah. Duplicate of: http://stackoverflow.com/questions/10834665/how-can-i-handle-time-zones-in-my-webapp – Aaron Digulla Jan 23 '14 at 13:23
  • 1
    Additional info: Use UTC (not GMT) to store the data. You can get the value to save by calling `getMillis()`. When you want to display the data, use the millisecond value plus the target time zone. – Aaron Digulla Jan 23 '14 at 13:25
  • I am sorry can you elaborate getMillis() part? Do you mean saving Date/ time in millis format in DB then retrieving it and converting it from millis to Date/time again to the user? – MChan Jan 23 '14 at 13:57
  • Yes. The milliseconds are always relative to UTC and independent of the time zone. That means if 10 people around the globe ask Java for `System.currentTimeMillis()` right now, we would all get the same number (well, yeah, more or less). When you create a `Date` from that, you mix the independent millis with the local time zone to get a local date. Internally, a timezone is simply an offset to UTC. So when the date is formatted, it will do `millis+timezone.offset` first. – Aaron Digulla Jan 23 '14 at 14:06
  • And using the millis approach, I can still get DateTime to display the date in different formats also (ex. MM/DD/YYYY or YYYY-MM-DD...etc)...correct? – MChan Jan 23 '14 at 14:16
  • Final question, will I still need to write a customer Hibernate mapper to save/ retrieve Date/time in millis? – MChan Jan 23 '14 at 14:18
  • You need a hibernate mapper if you want to save only the millis in the database. – Aaron Digulla Jan 23 '14 at 14:52
  • 1
    @MChan RE: display DateTime in different formats… Yes. Joda-Time offers standard ISO 8601 formats, other built-in formats, Locale-sensitive formats, and you can define your own formats. Search StackOverflow for "joda format" for many examples. – Basil Bourque Jan 24 '14 at 07:11
  • @AaronDigulla Your answer and comments are clear and enlightening, except… I don't understand your statement `Use UTC (not GMT) to store the data`. The only difference between the two (UTC & GMT) is leap-seconds. And both Java & Joda-Time ignore leap seconds, which mean they *really* use GMT despite their "UTC" labels. So to what are you referring? – Basil Bourque Jan 24 '14 at 07:15
  • Sorry for the confusion. You're right, for most practical purposes the two are the same. But some people mean "GMT" == GMT date format. Also, most documentation in Java itself prefers UTC. It's interesting to see that Joda-Time avoids the terms when it can (see the quick start guide and JavaDoc for DateTime). Anyone interested in the details of the distinction between UTC and GMT should have a look at the JavaDoc for `java.lang.Date`. – Aaron Digulla Jan 24 '14 at 09:10