Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Time Zones in Java / GWT (Client-side)

Tags:

java

gwt

[Client-side GWT class]

I have a Date Object...

Date dataObject = DateTimeFormat.getFormat("yyyy-MM-dd'T'HH:mm:ss.SSS")
                      .parse("2009-10-12T00:00:00.000);

This works fine. However when I do a:

dateObject.getTime();

It returns a UNIX Time milliseconds using a GMT with daylight savings, therefore making it a UNIX Time I cannot use. I need it in UTC. How do I do this?


Currently I'm parsing a date and it is giving me back:

'Thu Apr 16 08:46:20 GMT+100 2009' @ '1239867980191'

However the date I'm passing in is 1 hour less than this time (7:46 and not 8:46!).

How do I pass in the fact it's UTC? Or if it can't use UTC (which would be ridiculous), how do I use GMT without the daylight savings?

like image 267
Federer Avatar asked Jan 28 '10 15:01

Federer


People also ask

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.

How do I set US time zone in Java?

SimpleDateFormat myDate = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss"); myDate. setTimeZone(TimeZone. getTimeZone("UTC")); Date newDate = myDate. parse("2010-05-23T09:01:02");

What TimeZone does Java date use?

Actually, Date in Java is always in GMT and it represents a number of milliseconds since 01-01-1970 00:00 and when we print Date, it calls the toString method and displays date-time information in the local timezone.


2 Answers

Your last edit makes things clearer.

Basically, you are confused, and you already get what you want.

1239867980191 milliseconds since the Epoch translates to Thursday, April 16th, 2009, at 7:46:20.191 in the GMT time zone. The very same instant translates to the same day, but 8:46:20.191 in the GMT+01 time zone. If your input string specified "7:46:20.191" and you indeed got 1239867980191 from Date.getTime() then congratulations, the parsing code understood your "7:46:20.191" as to be interpreted in the GMT time zone, and did it properly.

If afterwards you get "8:46:20" when printing, this is only because you use the GMT+01 time zone for displaying that instant. Note that the string contains GMT+100 precisely to notify you that it uses that time zone for display purposes. The instant which the Date instance represents is nonetheless exactly the instant you wish it to contain. Remember that a Date instance represents an instant in time, for which no notion of time zone applies: time zones are used to convert instants into calendar elements (days, hours...) and back.

To convert a Date to a displayable string, use DateTimeFormat.format(Date, TimeZone) which lets you specify which time zone you want to use for that string.

like image 191
Thomas Pornin Avatar answered Sep 18 '22 19:09

Thomas Pornin


Since the Calendar class is not supported in GWT, maybe something hackish like this will work:

final String timezone = "GMT-07:00";
DateTimeFormat dtf = DateTimeFormat.getFormat("yyyy-MM-dd'T'HH:mm:ssZZZZ");
long unix = dtf.parse("2009-10-12T00:00:00" + timezone).getTime();

This way you can provide the correct timezone info - though, that should be the default behaviour.

like image 38
Igor Klimer Avatar answered Sep 17 '22 19:09

Igor Klimer