我正在将时间戳转换为日期格式 yyyy-MM-dd HH:mm:ss.SSS
我正在使用 America/New_York
如TimeZone
。每当我将时间戳转换为日期时,它都会显示比平常的日期和时间少一小时。如何在java中解决这个问题?
代码如下:
String timestamp = "1431941838000";
long time = Long.valueOf(timestamp);
Date currentDate = new Date(time);
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
TimeZone zone = TimeZone.getTimeZone("America/New_York");
df.setTimeZone(zone);
String finale = df.format(currentDate);
最佳答案
尝试使用EST
替换America/New_York
,例如
TimeZone zone = TimeZone.getTimeZone("EST");
已更新
这是我的测试代码:
String timestamp = "1431941838000";
long time = Long.valueOf(timestamp);
Date currentDate = new Date(time);
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
TimeZone zoneNewYork = TimeZone.getTimeZone("America/New_York");
df.setTimeZone(zoneNewYork);
String finale = df.format(currentDate);
System.out.println(finale);
TimeZone zoneEst = TimeZone.getTimeZone("EST");
df.setTimeZone(zoneEst);
finale = df.format(currentDate);
System.out.println(finale);
我的结果如下:
2015-05-18 05:37:18.000
2015-05-18 04:37:18.000
关于java - java中的时间戳转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30299930/