baba.kabira
baba.kabira

Reputation: 3171

Current est time in java with daylight saving

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.

Upvotes: 6

Views: 7144

Answers (1)

Andrew Spencer
Andrew Spencer

Reputation: 16484

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

Upvotes: 1

Related Questions