Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to tackle daylight savings using TimeZone in Java

I have to print the EST time in my Java application. I had set the time zone to EST using:

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

But when the daylight savings is being followed in this timezone, my code does not print the correct time (it prints 1 hour less).

How to make the code work to read the correct time always, irrespective of whether the daylight savings are being observed or not?

PS: I tried setting the timezone to EDT, but it doesn't solve the problem.

like image 693
Surya Chandra Avatar asked May 11 '12 05:05

Surya Chandra


People also ask

How does time zones work with daylight savings?

The simplest way to remember "time zone math" for the states that recognize Daylight saving time is three-two-one: three hours difference from Eastern to Pacific, two hours difference from Eastern to Mountain, and one hour difference from Eastern to Central.

How do you counteract Daylight Savings Time?

Start preparing a few days early. About a week before “springing forward,” Dr. Walia recommends that you start going to bed 15 to 30 minutes earlier than your usual bedtime. Your body needs that bit of extra time to make up for the lost hour.

How does Java handle time zone issues?

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.

Is DST in Java?

Occasionally, countries change their DST start and end dates. These changes can affect the date and time functions in applications, because the original start and end dates are programmed into the operating system and in Java software.


2 Answers

This is the problem to start with:

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

The 3-letter abbreviations should be wholeheartedly avoided in favour of TZDB zone IDs. EST is Eastern Standard Time - and Standard time never observes DST; it's not really a full time zone name. It's the name used for part of a time zone. (Unfortunately I haven't come across a good term for this "half time zone" concept.)

You want a full time zone name. For example, America/New_York is in the Eastern time zone:

TimeZone zone = TimeZone.getTimeZone("America/New_York"); DateFormat format = DateFormat.getDateTimeInstance(); format.setTimeZone(zone);  System.out.println(format.format(new Date())); 
like image 170
Jon Skeet Avatar answered Sep 30 '22 18:09

Jon Skeet


Other answers are correct, especially the one by Jon Skeet, but outdated.

java.time

These old date-time classes have been supplanted by the java.time framework built into Java 8 and later.

If you simply want the current time in UTC, use the Instant class.

Instant now = Instant.now(); 

EST is not a time zone, as explained in the correct Answer by Jon Skeet. Such 3-4 letter codes are neither standardized nor unique, and further the confusion over Daylight Saving Time (DST). Use a proper time zone name in the "continent/region" format.

Perhaps you meant Eastern Standard Time in east coast of north America? Or Egypt Standard Time? Or European Standard Time?

ZoneId zoneId = ZoneId.of( "America/New_York" ); ZoneId zoneId = ZoneId.of( "Africa/Cairo" ); ZoneId zoneId = ZoneId.of( "Europe/Lisbon" ); 

Use any such ZoneId object to get the current moment adjusted to a particular time zone to produce a ZonedDateTime object.

ZonedDateTime zdt = ZonedDateTime.now( zoneId ) ; 

Adjust that ZonedDateTime into a different time zone by producing another ZonedDateTime object from the first. The java.time framework uses immutable objects rather than changing (mutating) existing objects.

ZonedDateTime zdtGuam = zdt.withZoneSameInstant( ZoneId.of( "Pacific/Guam" ) ) ; 

Table of date-time types in Java, both modern and legacy.

like image 33
Basil Bourque Avatar answered Sep 30 '22 17:09

Basil Bourque