3

我有以下格式的日期对象:

2013 年 1 月 20 日星期日 10:12:27 GMT+02:00

上面的时间正确地出现在 microsoft Outlook 中:

2013 年 1 月 20 日星期日 12:12 PM(这是 GMT+2 >> 客户端时区的时间)

当尝试将日期对象格式化SimpleDateFormat为在 Outlook 中显示时,使用以下代码:

SimpleDateFormat sdf=new SimpleDateFormat(
    "EEE M/d/yyyy hh:mm a");
    String receivedDate = sdf.format(email.getDateTimeReceived());

格式化的结果是:

2013 年 1 月 20 日星期日上午 10:12

所以缺少时区差异的两个小时。

请告知如何解决这个问题,谢谢。

4

2 回答 2

4

如果我理解正确,您想使用 GMT 时区格式化日期。

DateFormat dateFormat = new SimpleDateFormat("EEE M/d/yyyy hh:mm a");
dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
String formattedDate = dateFormat.format(date);
于 2013-01-20T11:04:55.137 回答
3

你忘了告诉SimpleDateFormat包括时区信息。

这可以解决问题:

SimpleDateFormat sdf=new SimpleDateFormat(
    "EEE M/d/yyyy hh:mm a zzzZ yyyy");

注意最后的大写Z以显示时间差异

这将打印:

2013 年 1 月 20 日星期日 12:09 PM CET+0100 2013

如果你需要它是格林威治标准时间,你可以这样强制:

sdf.setTimeZone(TimeZone.getTimeZone("GMT"));

现在它将打印:

2013 年 1 月 20 日星期日上午 11:11 GMT+0000 2013

如果您不需要 AM/PM,只需删除a

于 2013-01-20T11:03:46.787 回答