Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Current est time in java with daylight saving

Tags:

java

date

time

I stumbled upon this piece of code to get current time in EST with daylight saving. It seems to work fine, when checked on the internet by the time displayed in sites providing current time in EST with daylight saving.

  SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MMM-dd HH:mm:ss.SSS"); 
  dateFormat.setTimeZone(TimeZone.getTimeZone("EST5EDT")); 
  System.out.println(dateFormat.format(new Date()));

I just want to confirm, has anybody used something like this before or there is a better way to achieve the same. I cannot use Joda library due to constraints.

Thanks in advance.

like image 383
baba.kabira Avatar asked Oct 20 '10 03:10

baba.kabira


People also ask

Is est currently in daylight savings time?

The EST time zone has never observed Daylight Saving Time. There are no future Daylight Saving Time rules for this time zone.

How do you get daylight savings in Java?

The right way to handle DST in Java is to instantiate a Timezone with a specific TZDB Timezone ID, eg. “Europe/Rome”. Then, we'll use this in conjunction with time-specific classes like java.

How do you know if a time zone is eligible for daylight savings in Java?

5. How to identify if a timezone is eligible for DayLight Saving? Explanation: public abstract boolean useDaylightTime() is provided in TimeZone class.

Does UTC to EST change with daylight savings?

However, you need to know what happens during daylight saving time in the United States. In short, the local time is advanced one hour during daylight saving time. As an example, the Eastern Time zone difference from UTC is -4 hours during daylight saving time rather than -5 hours as it is during standard time.


1 Answers

Yes, that's the best (and indeed the only) way to do this using Java's standard libraries.

like image 152
Andrew Spencer Avatar answered Sep 21 '22 18:09

Andrew Spencer