从服务器转换UTC日期为本地时间
问题描述:
我从服务器从服务器转换UTC日期为本地时间
2016-06-11T11:14:57.000Z
既然是UTC得到这个字符串作为日期,我想转换我的本地时间。
SimpleDateFormat mFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
SimpleDateFormat endFormat = new SimpleDateFormat("hh:mm a");
mFormat.setTimeZone(TimeZone.getTimeZone("GMT+5:00"));
Date date = mFormat.parse(mBooking.startTime);
但是日期转换为2:00AM
现在我不明白为什么11am
是越来越转化为2:00AM
难道我做错了什么?
答
因为你没有正确设置时区,以每个SimpleDateFormat
确实mFormat
应设置为UTC
和endFormat
到GMT + 5
,这里是你应该做的事情:
SimpleDateFormat mFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
// Set UTC to my original date format as it is my input TimeZone
mFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
Date date = mFormat.parse("2016-06-11T11:14:57.000Z");
SimpleDateFormat endFormat = new SimpleDateFormat("hh:mm a");
// Set GMT + 5 to my target date format as it is my output TimeZone
endFormat.setTimeZone(TimeZone.getTimeZone("GMT+5:00"));
System.out.println(endFormat.format(date));
输出:
04:14 PM
+0
完美,谢谢我看到现在的错误:) –
+0
npbr,不客气 –
无法重现。运行这段代码给了我一个星期六的日期,2016年6月11日06:14:57 UTC。你没有在你的问题中列出你如何使用/读取'date'的值。你在哪里看到凌晨2点的价值? – Sam