0

我需要将当前日期时间转换为 ios8601 格式,例如:

日期和时间格式:01 Jan 2014 BST 12:00 PM

最终在我的 jsp 上,我希望将格式化的日期放入

<time datetime="{date and time in ISO8601}">{date and time in format: 01 Jan 2014 BST 12:00 PM}</time>

我目前的尝试:

    TimeZone tz = TimeZone.getTimeZone("BST");
    DateFormat df = new SimpleDateFormat("dd-MM-yyyy'T'HH:mm'Z'");
    df.setTimeZone(tz);
    String nowAsISO = df.format(new Date());

但结果看起来不像我期望得到的......

4

1 回答 1

2

此模式将为您提供所需的:

SimpleDateFormat format = new SimpleDateFormat("dd MMM yyyy zzz HH:mm a");
format.setTimeZone(TimeZone.getTimeZone("Europe/London"));
System.out.println(format.format(new Date()));

印刷:

01 Sep 2014 BST 15:37 PM

注意:我假设您正在寻找 BST = 英国夏令时,应该如上所示。如果您想要孟加拉国时间,请Asia/Dhaka用作时区指示符。另请注意,英国夏令时在 1 月 1 日无效(如您的示例中所引用)。

于 2014-09-01T14:35:07.680 回答