Joda Time provides a quality replacement for the Java date and time classes. The design allows for multiple calendar systems, while still providing a simple API. Converting a Joda DateTime object from one timezone to another is pretty easy and saves you quite a number of lines of code. See method below.
|
1 2 3 4 5 |
public static Date convertJodaTimezone(LocalDateTime date, String srcTz, String destTz) { DateTime srcDateTime = date.toDateTime(DateTimeZone.forID(srcTz)); DateTime dstDateTime = srcDateTime.withZone(DateTimeZone.forID(destTz)); return dstDateTime.toLocalDateTime().toDateTime().toDate(); } |
Since Joda Time has its own class for storing Date and Calendar objects, the DateTime class has its own toDate() method for converting it to a java.util.Date.