Reputation: 2497
I want to get the eastern standard time /EST current time but it keeps on showing my current time... Any idea what I am doing wrong?
DateFormat format = new SimpleDateFormat("yyyy.MM.dd G 'at' HH:mm:ss z");
TimeZone utc = TimeZone.getTimeZone("America/New_York");
GregorianCalendar gc = new GregorianCalendar(utc);
Date now = gc.getTime();
String strDateString = format.format(now);
Upvotes: 1
Views: 1533
Reputation: 10163
The Date
class is intended to reflect coordinated universal time (UTC), so it does not reflect timezone you use in GregorianCalendar
. Also by default SimpleDateFormat
uses system default timezone and if you want to override it you should do it explicitly:
DateFormat format = new SimpleDateFormat("yyyy.MM.dd G 'at' HH:mm:ss z");
TimeZone la = TimeZone.getTimeZone("America/Los_Angeles");
format.setTimeZone(la);
Another option is to switch to Joda Time library:
DateTimeZone la = DateTimeZone.forTimeZone(TimeZone.getTimeZone("America/Los_Angeles"));
DateTimeFormat.forPattern(dateTimeFormat).withZone(la).print(System.currentTimeMillis());
Upvotes: 2