Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linux server showing UTC instead of EST, local showing EST

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());
    }
like image 334
Mike Flynn Avatar asked Dec 02 '15 15:12

Mike Flynn


1 Answers

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.)

like image 131
Jon Skeet Avatar answered Nov 02 '22 07:11

Jon Skeet