java.util.Date
String Output is Not UTC
From your Question:
it shows, Wed Oct 02 11:34:26 IST 2013. It should be in the UTC format.
No it should not be in UTC format. That may be what you want, but that is not how the class’ behavior is documented.
Your code:
System.out.println(dateTime.toDate());
…is first converting a Joda-Time DateTime
to a java.util.Date
object, and then implicitly calling the toString
method on that j.u.Date object. The documented behavior of a j.u.Date for its toString
method is to apply the JVM's currently default time zone to the generated string. Internally, the j.u.Date is indeed in UTC, but its generated string is not. This behavior of its toString
was a well-intentioned but ultimately very poor design decision of the Java team that has caused no end of confusion to countless programmers. But it the documented behavior. You should always carefully read a library's documentation rather than assume the library behaves the way you imagine.
Show Joda-Time DateTime
In UTC
From your Question:
Is there a way to represent a Date in UTC after converting it from Joda-Time?
Yes, generating a string representation of a Joda-Time DateTime
in UTC is easy and clear, unlike dealing with java.util.Date/.Calendar/SimpleDateFormat. Simply call withZone
to instantiate a new DateTime based on the original but adjusted to another time zone.
A little example source code in Joda-Time 2.5 should make it obvious. Also search StackOverflow for hundreds of more examples using Joda-Time.
DateTimeZone zoneMontréal = DateTimeZone.forID( "America/Montreal" );
DateTime nowInMontréal = DateTime.now( zoneMontréal );
DateTimeZone zoneKolkata = DateTimeZone.forID( "Asia/Kolkata" );
DateTime nowInKolkata = nowInMontréal.withZone( zoneKolkata ); // Instantiating a new date-time object based on the original but adjusted for another time zone.
DateTime nowInUtc = nowInMontréal.withZone( DateTimeZone.UTC ); // Ditto, new object, adjusted time zone.
When dumped to console by calling toString
on each DateTime:
nowInMontréal: 2014-11-14T02:03:19.932-05:00
nowInKolkata: 2014-11-14T12:33:19.932+05:30
nowInUtc: 2014-11-14T07:03:19.932Z
All three of these represent the same simultaneous moment in the timeline of the Universe. Montréal time is five hours behind UTC, India time is five and a half hours ahead of UTC. Same moment, but different wall clock times.
Put a Joda-Time DateTime
in Database
From your Question:
I want a date to be stored into a database as represented by the UTC zone. org.joda.time.DateTime needs to be converted to java.util.Date (or java.sql.Timestamp) before inserting it to a database. How to overcome this situation?
Easy. Construct a java.sql.Timestamp object by passing the count of milliseconds since Unix epoch. Note that count must be a long
not an int
.
java.sql.Timestamp ts = new java.sql.Timestamp( dateTime.getMillis() );
Tip: Be aware of the nature of your database's date-time data types. Postgres for example has excellent support for such types including those defined by the SQL spec. Many database do not, sporting old legacy types or only a few weak types. Read the doc and experiment to make sure you understand the behavior.