Skip to content Skip to sidebar Skip to footer

Can't Get Australian Date/time

I am using the following code to generate the correct Australian date/time. But it continues to return GMT time. Any ideas would be great Date currentTime = Calendar.getInstance(T

Solution 1:

Try this:

Calendarcalendar=newGregorianCalendar();
TimeZonetimeZone= TimeZone.getTimeZone("Australia/Sydney");
calendar.setTimeZone(timeZone);

DatecurrentTime= calendar.getTime();

Check out this tutorial for a list of all time zones with a few more examples.

Solution 2:

(a) Australia has a few different time zones with different offsets. See this list.

You should specify a time zone by name rather than offset so that your date-time library may assist with Daylight Saving Time or other anomalies.

(b) caseyscarborough gave a correct answer. Here is the same kind of code but in Joda-Time 2.3.

org.joda.time.DateTimeZoneadelaideTimeZone= org.joda.time.DateTimeZone.forID( "Australia/Adelaide" );
org.joda.time.DateTimeadelaideDateTime=neworg.joda.time.DateTime( adelaideTimeZone ); // Standard time is +09:30 of UTC.
System.out.println( "Now in Adelaide: " + adelaideDateTime );
System.out.println( "Same moment in UTC: " + adelaideDateTime.toDateTime( org.joda.time.DateTimeZone.UTC ) );

When run…

Now in Adelaide: 2013-11-28T14:21:21.192+10:30
Same moment in UTC: 2013-11-28T03:51:21.192Z

Post a Comment for "Can't Get Australian Date/time"