TimeZone utcTimezone_ = TimeZone.getTimeZone("UTC");
TimeZone _utc = TimeZone.getTimeZone("Zulu");
DateFormat _fo = new SimpleDateFormat("yyyy-MM-dd HH:mm");
_fo.setTimeZone(_utc);
GregorianCalendar gc = new GregorianCalendar(_utc);
System.out.println(gc.getTime());
System.out.println(gc.get(GregorianCalendar.MINUTE));
System.out.println(gc.get(GregorianCalendar.HOUR_OF_DAY));
An example of the output when running the above code is:
Mon Sep 16 16:40:37 CST 2013
10
7
In this case I am expecting minutes to be 40 (not 10) and hour to be 16 (not 7). What don't I comprehend about all this at the moment? Thanks.
EDIT: If I change the timezone to CST which is my local timezone the minute and hour still do not match:
TimeZone _utc = TimeZone.getTimeZone("CST");
DateFormat _fo = new SimpleDateFormat("yyyy-MM-dd HH:mm");
_fo.setTimeZone(_utc);
GregorianCalendar gc = new GregorianCalendar(_utc);
System.out.println(gc.getTime());
System.out.println(gc.get(GregorianCalendar.MINUTE));
System.out.println(gc.get(GregorianCalendar.HOUR_OF_DAY));
Output is:
Mon Sep 16 21:23:48 CST 2013
53
6
System.out.println(gc.getTime());
This line prints the calendar in your computer local timezone (getTime() returns a Date containing the number of ms since epoch that is not timezone-related, then the implicit toString() call print it using local timezone). It is CST according to the result.
System.out.println(gc.get(GregorianCalendar.MINUTE));
System.out.println(gc.get(GregorianCalendar.HOUR_OF_DAY));
These lines print the calendar fields in the calendar timezone. It is "Zulu" according to your code.
By the way, it is strongly recommended not to use three-letters timezones:
Three-letter time zone IDs
For compatibility with JDK 1.1.x, some other three-letter time zone IDs (such as "PST", "CTT", "AST") are also supported. However, their use is deprecated because the same abbreviation is often used for multiple time zones (for example, "CST" could be U.S. "Central Standard Time" and "China Standard Time"), and the Java platform can then only recognize one of them.
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