0

I'm new to time zones in Java

I get from the server, the following String:

`"startDateTime": "2014-08-10T20:08:09.8948Z"`

I'm not sure what does the Z mean? How can I know from my android app

in what time zone did the server saved the date?

Today I use this code:

public class DateFormatter implements JsonDeserializer {

@Override
public Date deserialize(JsonElement element, Type arg1,
        JsonDeserializationContext arg2) throws JsonParseException {
    String date = element.getAsString();

    SimpleDateFormat formatter = new SimpleDateFormat(
            "yyyy-MM-dd'T'HH:mm:ss.SSS", Locale.getDefault());
    // formatter.setTimeZone(TimeZone.getDefault());

    try {
        return formatter.parse(date);
    } catch (ParseException exp) {
        // System.err.println("Failed to parse Date:", exp);
        return null;
    }
}

public String toStringFormat(Date date) throws JsonParseException {

    SimpleDateFormat formatter = new SimpleDateFormat("HH:mm dd/mm/yy",
            Locale.getDefault());
    // formatter.setTimeZone(TimeZone.getDefault());
    return formatter.format(date);
}

}

but what does Locale.getDefault returns? How does it know in which time zone am I ?

Is there any reliable and easy to use open source jar to ease the time zones shifting?

Elad Benda
  • 35,076
  • 87
  • 265
  • 471
  • 1
    Z stands for the military Zulu time, more commonly known as GMT (Greenwich Mean Time) – Filip Aug 30 '14 at 15:20

2 Answers2

1

The 'Z' is used for Coordinated Universal Time, which is effectively the same as GMT.

Locale.getDefault() just gives you the default Locale, as explained in the JavaDoc (http://docs.oracle.com/javase/7/docs/api/java/util/Locale.html).

TimeZone.getDefault() will get you the current time zone.

As far as shifting time zones, you can use standard Java. There's a discussion here: How to set time zone of a java.util.Date?

Community
  • 1
  • 1
  • thanks. How come `SimpleDateFormat` ctor gets `Locale` as second parameter and no just `TimeZone`? how will it know the time zone when the locale is english-us? – Elad Benda Aug 31 '14 at 06:58
  • SimpleDateFormat takes Locale in the constructor to determine the Locale specific visual presentation of the date format, but it’s not possible to determine time zone based on Locale alone, you need to get the TimeZone when it’s needed. – Jonathan Scholis Aug 31 '14 at 14:06
0

Joda-Time - Java date and time API is having a lots of features to play around with date and time.

Check the following link also Stackoverflow - In joda time how to convert time zone without changing time

Community
  • 1
  • 1
Benoy Prakash
  • 436
  • 5
  • 17