I am having trouble figuring out why the timezone for the code below keeps showing UTC instead of EST. On my local computer it show EST, even if I am in MST time but on the actual server it keeps showing UTC. Any clue?
Mon Nov 9 2015 1:58:49 PM UTC
@JsonIgnore
public String getDateCreatedFormatted() {
Calendar calendar = Calendar.getInstance();
calendar.setTime(getDateCreated());
calendar.setTimeZone(TimeZone.getTimeZone("EST"));
SimpleDateFormat format = new SimpleDateFormat("EEE MMM d yyyy h:mm:ss a z");
return format.format(calendar.getTime());
}
You've set the calendar to EST, but you haven't set the time zone on the SimpleDateFormat
, which is the one use for formatting. Just use:
format.setTimeZone(TimeZone.getTimeZone("America/New_York"));
before you format the Date
. You also don't need the Calendar
at all, by the looks of it:
@JsonIgnore
public String getDateCreatedFormatted() {
SimpleDateFormat format = new SimpleDateFormat("EEE MMM d yyyy h:mm:ss a z", Locale.US);
format.setTimeZone(TimeZone.getTimeZone("America/New_York"));
return format.format(getDateCreated());
}
Also I would strongly advise you to use full time zone IDs as above, rather than the abbreviations like "EST" which are ambiguous. (There are two problems there - firstly, EST can mean different things in different locations; secondly, the US EST should always mean Eastern standard time, whereas I assume you want to format using Eastern time, either standard or daylight depending on whether daylight saving time is in effect or not.)
See more on this question at Stackoverflow