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.
The EST time zone has never observed Daylight Saving Time. There are no future Daylight Saving Time rules for this time zone.
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.
5. How to identify if a timezone is eligible for DayLight Saving? Explanation: public abstract boolean useDaylightTime() is provided in TimeZone class.
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.
Yes, that's the best (and indeed the only) way to do this using Java's standard libraries.
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