我很难弄清楚为什么下面代码的时区总是显示UTC而不是EST。在我的本地计算机上,它显示EST,即使我在MST时间,但在实际服务器上,它一直显示UTC。有线索吗?
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());
}发布于 2015-12-02 15:27:41
您已经将日历设置为EST,但还没有在SimpleDateFormat上设置时区,这是格式化的唯一用途。只需使用:
format.setTimeZone(TimeZone.getTimeZone("America/New_York"));在格式化Date之前。从外观上看,您也根本不需要Calendar:
@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());
}另外,我强烈建议您像上面那样使用完整的时区ID,而不是像"EST“这样模棱两可的缩略语。(这里有两个问题--第一,在不同的地点,EST可能意味着不同的东西;其次,美国的EST应该总是指东方标准时间,而我假设你想使用东方时的格式,不管是标准的还是日光的,这取决于夏令时是否有效。)
https://stackoverflow.com/questions/34046380
复制相似问题