I've been trying to convert a server's date and time to user's one with the following code
@Test
public void playingWithJodaTime() {
LocalDateTime localDateTime = new LocalDateTime();
System.out.println("server localDateTime : "
+ localDateTime.toDateTime(DateTimeZone.getDefault()).toDate());
System.out.println("user's localDateTime : "
+ localDateTime.toDateTime(DateTimeZone.forID("Asia/Jakarta"))
.toDate());
}
Printed result
server localDateTime : Tue Dec 17 00:04:29 SGT 2013
user's localDateTime : Tue Dec 17 01:04:29 SGT 2013
However the printed result is not like what I expected since the server time zone is (UTC+08:00) Kuala Lumpur, Singapore
while the user's is (UTC+07:00) Bangkok, Hanoi, Jakarta
.
What did I do wrong here ?
You can make use of the following DateFormat. SimpleDateFormat myDate = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss"); myDate. setTimeZone(TimeZone. getTimeZone("UTC")); Date newDate = myDate.
If you cannot change the OS or the JVM timezone, you can still convert a Java Date/Time or Timestamp to a specific time zone using the following two JDBC methods: PreparedStatement#setTimestamp(int parameterIndex, Timestamp x, Calendar cal) – to convert the timestamp that goes to the database.
The java. util. Date has no concept of time zone, and only represents the number of seconds passed since the Unix epoch time – 1970-01-01T00:00:00Z. But, if you print the Date object directly, the Date object will be always printed with the default system time zone.
According to javadoc the java. util. Date class represents number of milliseconds since the standard base time known as "the epoch", namely 1 January 1970, 00:00:00 GMT.
You are converting DateTime to java Date (why?).java.util.Date
uses default JVM's time zone
So, you missed time zone at this conversion.
This example works as expected:
LocalDateTime localDateTime = new LocalDateTime();
System.out.println("server localDateTime : "
+ localDateTime.toDateTime(DateTimeZone.getDefault()));
System.out.println("user's localDateTime : "
+ localDateTime.toDateTime(DateTimeZone.forID("Asia/Jakarta")));
if you want to convert joda DateTime to something else, then convert it to Calendar
LocalDateTime localDateTime = new LocalDateTime();
System.out.println("server localDateTime : "
+ localDateTime.toDateTime(DateTimeZone.getDefault()).toGregorianCalendar());
System.out.println("user's localDateTime : "
+ localDateTime.toDateTime(DateTimeZone.forID("Asia/Jakarta")).toGregorianCalendar());
The error is the use of toDate(). Why? By saying toDate() you convert your LocalDateTime to a java.util.Date which is timezone-independent. But then you use implicitly the toString()-method on j.u.Date which uses your default timezone, so in both cases you get the same representation.
The solution is just to leave out the call of toDate(). JodaTime objects have better toString()-implementations closely following ISO standard and will print the result in different timezones as you have specified.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With