Java 在 LocalDate 和 XMLGregorianCalendar 之间转换

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29767084/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 08:33:40  来源:igfitidea点击:

Convert between LocalDate and XMLGregorianCalendar

javajava-8converterjava-time

提问by maja

What's the best way to convert between LocalDatefrom Java 8 and XMLGregorianCalendar?

LocalDateJava 8 和之间进行转换的最佳方法是XMLGregorianCalendar什么?

采纳答案by JodaStephen

Converting from LocalDateto XMLGregorianCalendar:

转换LocalDateXMLGregorianCalendar

LocalDate date = LocalDate.now();
GregorianCalendar gcal = GregorianCalendar.from(date.atStartOfDay(ZoneId.systemDefault()));
XMLGregorianCalendar xcal = DatatypeFactory.newInstance().newXMLGregorianCalendar(gcal);

Converting back is simpler:

转换回来更简单:

xcal.toGregorianCalendar().toZonedDateTime().toLocalDate();

回答by maja

To convert from LocalDateto XMLGregorianCalendaryou can use

要从转换LocalDateXMLGregorianCalendar您可以使用

LocalDate localDate = ...;
GregorianCalendar calender = new GregorianCalendar();
Date utilDate = Date.from( localDate.atStartOfDay( ZoneId.systemDefault() ).toInstant() );
calender.setTime(utilDate);     
XMLGregorianCalendar xmlCal = DatatypeFactory.newInstance().newXMLGregorianCalendar(calender);

And to convert XMLGregorianCalendarback to LocalDate:

并转换XMLGregorianCalendarLocalDate

XMLGregorianCalendar xmlCal = ...;
Date utilDate = xmlCal.toGregorianCalendar().getTime();
LocalDate localDate = LocalDateTime.ofInstant( utilDate.toInstant(), ZoneId.systemDefault() ).toLocalDate();

回答by riskop

The LocalDate stores only year/month/day information. There is no time nor time-zone information in it. The XMLGregorianCalendar stores date (year/month/day) + optionallytime and optionallytime zone information.

LocalDate 仅存储年/月/日信息。其中没有时间或时区信息。XMLGregorianCalendar 存储日期(年/月/日)+可选的时间和可选的时区信息。

So converting from LocalDate to XMLGregorianCalendar is simple:

所以从 LocalDate 转换为 XMLGregorianCalendar 很简单:

LocalDate in;
XMLGregorianCalendar out;
in = LocalDate.parse("1999-11-11");
out = DatatypeFactory.newInstance().newXMLGregorianCalendar(in.toString());

Converting from XMLGregorianCalendar to LocalDate might be not so simple, because XMLGregorianCalendar mayhave time and time-zone information which you simply can't store in LocalDate.

从 XMLGregorianCalendar 转换为 LocalDate 可能不是那么简单,因为 XMLGregorianCalendar可能包含您根本无法存储在 LocalDate 中的时间和时区信息。

However, I guess that if you are converting from XMLGregorianCalendar to LocalDate then the XMLGregorianCalendar is resulting from a nontimezoned xsd:date element (represented as YYYY-MM-DD in the xml). In that case you should convert it like this:

但是,我想如果您从 XMLGregorianCalendar 转换为 LocalDate 那么 XMLGregorianCalendar 是由非时区 xsd:date 元素(在 xml 中表示为 YYYY-MM-DD)产生的。在这种情况下,您应该像这样转换它:

XMLGregorianCalendar in;
LocalDate out;
in = DatatypeFactory.newInstance().newXMLGregorianCalendar("2011-11-11");
out = LocalDate.parse(in.toXMLFormat());

Whole example:

整个例子:

    {
        LocalDate in;
        XMLGregorianCalendar out;
        in = LocalDate.parse("1999-11-11");
        out = DatatypeFactory.newInstance().newXMLGregorianCalendar(in.toString());
        System.out.println("in: " + in.toString());
        System.out.println("out: " + out.toXMLFormat());
    }
    {
        XMLGregorianCalendar in;
        LocalDate out;
        in = DatatypeFactory.newInstance().newXMLGregorianCalendar("2011-11-11");
        out = LocalDate.parse(in.toXMLFormat());
        System.out.println("in: " + in.toXMLFormat());
        System.out.println("out: " + out.toString());
    }

回答by Acanda

The following is a simple way to convert from LocalDate to XMLGregorianCalendar which both preserves the undefined fields (hours, timezone, etc.) and is efficient (i.e. no conversion to/from String). Unlike some of the other solutions this results in XML dates without timezones, e.g. <date>2018-11-06</date>instead of <date>2018-11-06+01:00</date>.

下面是一种从 LocalDate 转换为 XMLGregorianCalendar 的简单方法,它既保留了未定义的字段(小时、时区等),又高效(即不转换为/从字符串)。与其他一些解决方案不同,这会导致没有时区的 XML 日期,例如,<date>2018-11-06</date>而不是<date>2018-11-06+01:00</date>.

LocalDate date = ...;
XMLGregorianCalendar xmlCal = DatatypeFactory.newInstance().newXMLGregorianCalendar();
xmlCal.setYear(date.getYear());
xmlCal.setMonth(date.getMonthValue());
xmlCal.setDay(date.getDayOfMonth());

Converting back is a bit simpler:

转换回来更简单一点:

XMLGregorianCalendar xmlCal = ...
LocalDate date = LocalDate.of(xmlCal.getYear(), xmlCal.getMonth(), xmlCal.getDay());