Why does the calendar return the wrong hour with the correct time zone?


The returned time and date are correct except for the hour, which is 1 hour less than it should be.

It seems I am setting everything that is required to get the correct time and date:

- I'm using Calendar.getInstance(), instead of new Date() - I'm setting the timezone of the Calendar instance with Timezone.getTimeZone - I'm using DateFormat and SimpleDateFormat to format the output 


My time zone is Eastern Standard Time , aka UTC/GMT-5:00 . None of these lines have any effect:

  - cal.setTimeZone(TimeZone.getTimeZone(cal.getTimeZone().getDisplayName())); - cal.setTimeZone(TimeZone.getTimeZone("EST")); - cal.setTimeZone(TimeZone.getTimeZone("UTC")); - cal.setTimeZone(TimeZone.getTimeZone("GMT")); - cal.setTimeZone(TimeZone.getTimeZone("GMT-5:00")); 

... but each of these options sets my desired time zone.


Here is my attempt when I mistakenly add 1 hour to a Calendar instance:
 PrintWriter output = null; try { output = new PrintWriter( new BufferedWriter(new FileWriter("output.txt", true))); DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss:ms MM/dd/yyyy"); Calendar cal = Calendar.getInstance(); // ...doesn't seem to be working: cal.setTimeZone(TimeZone.getTimeZone(cal.getTimeZone().getDisplayName())); /* * adding an extra hour here, to make up for the incorrect hour value??? * ...without this line, everything is correct except the hour = n - 1: */ //cal.setTimeInMillis(cal.getTimeInMillis() + (1000 * 60 * 60)); // printing to console here: System.out.println(dateFormat.format(cal.getTime())); System.out.println(cal.getTimeZone().getDisplayName()); // printing to the log-file here: output.println(dateFormat.format(cal.getTime())); output.println(cal.getTimeZone().getDisplayName()); } catch (IOException e) { e.printStackTrace(); } finally { if (output != null) { output.close(); } } 


Exit:

 10:05:43:543 10/10/2013 GMT-05:00 

WRONG - it should be 11:05:43:543 ! (ps - Sorry, I can't use Joda-Time )

+8
java timezone datetime calendar
source share
3 answers

1) Set TimeZone to SimpleDateFormat

2) Either use UTC or the โ€œrealโ€ time zone (โ€œAustria / Viennaโ€),
(The name of the country, and the largest city in TimeZone, look at it to make sure)
EST (= UTC-5) is not a time zone that is very suitable for computing purposes, because it is time without daylight saving time.

Another example from central Europe:
In winter, we use MEZ (CET), in summer (summer time) we use (MESZ = CEST).
But you want the computer to calculate this for you, so don't use this:

Time zones are geological, so the name of the country is required.
Each country can decide to change its time zone whenever it wants (for example, Russia some time ago, and the Spaniard is discussing now.)

+3
source share

The calendar does not include daylight saving time.

According to this post: How to cope with summer savings using Timezone in java

3-letter abbreviations should be completely excluded in favor of TZDB zone identifiers. EST - Eastern Standard Time - and standard time never abides by DST; this is not really the full name of the time zone. This name is used for the time zone part. (Unfortunately, I did not find a good term for this โ€œhalf time zoneโ€ concept.)

Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("EST"));

This is a good place to start: http://download.java.net/jdk8/docs/api/java/time/ZoneId.html

+1
source share


For posterity, here is my improved code based on the message " How to cope with summer savings using Timezone in java ":

 PrintWriter output = null; try { output = new PrintWriter( new BufferedWriter(new FileWriter("output.txt", true))); // here are the relevant changes (a lot less code!): TimeZone zone = TimeZone.getTimeZone("America/New_York"); SimpleDateFormat simpleFormat = new SimpleDateFormat("HH:mm:ss:ms MM/dd/yyyy"); simpleFormat.setTimeZone(zone); // printing to console here: System.out.println(simpleFormat.format(new Date())); System.out.println(simpleFormat.getTimeZone().getDisplayName()); // printing to the log-file here: output.println(simpleFormat.format(new Date())); output.println(simpleFormat.getTimeZone().getDisplayName()); } catch (IOException e) { e.printStackTrace(); } finally { if (output != null) { output.close(); } } 


Note that I use SimpleDateFormat instead of DateFormat to adjust the output format (no need to use both).

0
source share

All Articles