将日期格式化为XML格式以包含UTC偏移量

3

我正在生成一个包含有效XML格式日期的XML,我需要它也包括UTC偏移量。

我使用的是groovy,但我将展示我使用的Java代码(任何一种语言的答案都可以):

Calendar c = Calendar.getInstance();  
long timeZoneOffset = c.timeZone.getOffset(c.getTimeInMillis())/(1000*60*60);
SimpleDateFormat formatter = new java.text.SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
formatter.format(c.getTime()) + "+0" + timeZoneOffset + ":00";

上述代码给出了我2011-06-12T07:23:25.000+03:00, 但是这个代码有两个问题:
  1. 它太难看了,也许不是最好的方法
  2. 对于印度(GMT +5:30)、尼泊尔 (GMT +5:45)等时区无效。
我尝试使用 new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss Z") 来处理时区,但它给我返回的是 2011-06-12T07:23:25.000+0300,这不是正确的格式(+0300 而不是 +03:00)。
还有其他的方法来满足我需要的日期格式吗?(最好不用第三方库)
5个回答

2

还有一种选择——也深藏在jaxb api中——(不需要Jodatime):

    Calendar c = ...
    String printDate = javax.xml.bind.DatatypeConverter.printDateTime(c);

HTH


谢谢 - 这是最简单的解决方案,可在安装了JRE6的任何地方使用。 - RonK

1

我认为最优雅的方法是使用Joda-Time库。您需要ISO 8601(第5.4节)格式(由xs:dateTime XSD类型表示):

 DateTime dt = new DateTime();
 DateTimeFormatter fmt = ISODateTimeFormat.dateTime();
 System.out.println(fmt.print(dt));

结果:

2011-06-12T07:36:32.294+02:00


0

0
你尝试过使用XMLGregorianCalendar吗?例如:
Calendar c = ...
DataTypeFactory f = DataTypeFactory.newInstance();
XMLGregorianCalendar xc = f.newXMLGregorianCalendar(c);
String str = xc.toXMLFormat();

如果日历是带有时区偏移的日期时间,则时区偏移应包含在结果字符串中,格式应符合XML数据类型规范。

0

java.time

现代日期时间API

演示:

import java.time.OffsetDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;

public class Main {
    public static void main(String[] args) {
        ZonedDateTime zdtNewYork = ZonedDateTime.now(ZoneId.of("America/New_York"));
        ZonedDateTime zdtIndia = ZonedDateTime.now(ZoneId.of("Asia/Kolkata"));
        ZonedDateTime zdtNepal = ZonedDateTime.now(ZoneId.of("Asia/Kathmandu"));
        System.out.println(zdtNewYork);
        System.out.println(zdtIndia);
        System.out.println(zdtNepal);

        // If you do not need to display the time zone name, convert it into
        // OffsetDateTime
        OffsetDateTime odtNewYork = zdtNewYork.toOffsetDateTime();
        System.out.println(odtNewYork);

        // Using DateTimeFormatter, you can display it in different formats
        String odtFormat = zdtNewYork.format(DateTimeFormatter.ISO_OFFSET_DATE_TIME.withLocale(Locale.ENGLISH));
        String dayMonFullNameFormat = zdtNewYork.format(DateTimeFormatter.ofPattern("EEEE d MMMM uuuu HH:mm:ssXXX"));
        System.out.println(odtFormat);
        System.out.println(dayMonFullNameFormat);
    }
}

输出:

2021-05-09T16:41:23.683709-04:00[America/New_York]
2021-05-10T02:11:23.685131+05:30[Asia/Kolkata]
2021-05-10T02:26:23.685187+05:45[Asia/Kathmandu]
2021-05-09T16:41:23.683709-04:00
2021-05-09T16:41:23.683709-04:00
Sunday 9 May 2021 16:41:23-04:00

了解更多关于现代日期时间 API*的内容,请访问教程:日期时间


*无论何种原因,如果您必须坚持使用Java 6或Java 7,可以使用 ThreeTen-Backport ,该工具将大部分java.time功能移植到Java 6和7中。如果您正在为Android项目工作,并且您的Android API级别仍不符合Java-8标准,请检查通过解糖可用的Java 8+ API以及如何在Android项目中使用ThreeTenABP


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接