Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use DateTimeZone in Java

Tags:

java

jodatime

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 ?

like image 558
abiieez Avatar asked Dec 16 '13 16:12

abiieez


People also ask

How do I set the TimeZone in Java?

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.

How does Java handle time zones?

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.

Does Java Date have time zone?

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.

What is the default TimeZone in Java?

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.


2 Answers

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());
like image 135
Ilya Avatar answered Oct 06 '22 00:10

Ilya


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.

like image 30
Meno Hochschild Avatar answered Oct 06 '22 01:10

Meno Hochschild