0

当我打印日期时,我可以看到 -6.00 的偏移量

2012-12-31T18:00:00 -0600

有没有一种干净的方法可以将偏移量添加到日期,我在java.util.Date.

我正在使用以下方法打印日期:

DateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss Z");
String date = sdf.format(new Date(dateInMilliseconds));
4

1 回答 1

2

偏移量与TimeZone.

如果您正在使用 a Calendar,则可以像这样获得 TimeZone :

Calendar cal = Calendar.getInstance();
TimeZone tz = cal.getTimeZone();

如果您有 TimeZone 实例,则可以像这样获取给定日期的偏移量:

int offset = tz.getOffset(cal.getTime().getTime());

当您问“我可以添加偏移量吗? ”时,我不确定您的意思。

如果你想标准化你的日期,你可以只使用 UTC 时间。

你可以用这样的 SimpleDateFormat 来做到这一点:

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
System.out.println(sdf.format(new Date()));

现在的输出:

  2013-06-25T19:09:10
于 2013-06-25T18:56:16.253 回答