I need to convert Joda-Time date/time to java.util.Date. I'm doing as follows.

DateTimeFormatter dateTimeFormatter=DateTimeFormat.forPattern("dd-MMM-yyyy hh:mm:ss aa");
DateTime dateTime = dateTimeFormatter.parseDateTime("2-Oct-2013 11:34:26 AM").withZone(DateTimeZone.UTC);

System.out.println(dateTime);

This displays date/time represented by UTC zone as expected. In this case, it is 2013-10-02T06:04:26.000Z

When this dateTime is converted to java.util.Date as follows,

System.out.println(dateTime.toDate());

it shows, Wed Oct 02 11:34:26 IST 2013. It should be in the UTC format.

Is there a way to represent a Date in UTC after converting it from Joda-Time?

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?

有帮助吗?

解决方案

A java.util.Date object per definition is always in UTC!
(timestamp is the number of millis since 1.1.1970 UTC)

But you have to set the timeZone to utc before time formating:

TimeZone utc = TimeZone.getTimeZone("UTC")

SimpleDateFormatter df = new SimpleDateFormatter (PATTERN);
df.setTimeZone(utc);

System.out.println(df.format(date));

In your code you used the java.util.Date.toString() method which uses your system default TimeZone.

其他提示

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.

public Date toDate(TimeZone timeZone)

Get the date time as a java.util.Date using the specified time zone.

The Date object created has exactly the same fields as this date-time, except when the time would be invalid due to a daylight savings gap. In that case, the time will be set to the earliest valid time after the gap.

In the case of a daylight savings overlap, the earlier instant is selected.

Converting to a JDK Date is full of complications as the JDK Date constructor doesn't behave as you might expect around DST transitions. This method works by taking a first guess and then adjusting. This also handles the situation where the JDK time zone data differs from the Joda-Time time zone data.

Unlike toDate(), this implementation does not rely on Java's synchronized time zone initialization logic, and should demonstrate better concurrent performance characteristics.

So this would look like:

Timezone utc = Timezone.getTimezone("UTC")
System.out.println(dateTime.toDate(utc));

let me know if it works.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top