简体   繁体   English

如何将 ZonedDateTime 格式化为 yyyy-MM-ddZ

[英]How to format ZonedDateTime to yyyy-MM-ddZ

I need to turn ZonedDateTime to XML Date data type, of format yyyy-MM-ddZ .我需要将 ZonedDateTime 转换为 XML 日期数据类型,格式为yyyy-MM-ddZ For example: 2020-02-14Z .例如: 2020-02-14Z I try to use DateTimeFormatter.ofPattern("yyyy-MM-ddZ") but the output is: 2020-02-14+0000 .我尝试使用DateTimeFormatter.ofPattern("yyyy-MM-ddZ")但输出是: 2020-02-14+0000 Which DateTimeFormatter pattern should I use to get the desired result?我应该使用哪种 DateTimeFormatter 模式来获得所需的结果?

DateTimeFormatter.ISO_OFFSET_DATE DateTimeFormatter.ISO_OFFSET_DATE

Use the built-in DateTimeFormatter.ISO_OFFSET_DATE .使用内置的DateTimeFormatter.ISO_OFFSET_DATE

    ZonedDateTime dateTime = ZonedDateTime.now(ZoneId.of("America/Fortaleza"));
    String dateForXml = dateTime.format(DateTimeFormatter.ISO_OFFSET_DATE);
    System.out.println(dateForXml);

When I ran this snippet just now, the output was:当我刚刚运行这个片段时,输出是:

2020-02-14-03:00 2020-02-14-03:00

If you want a string in UTC with a trailing Z , use ZoneOffset.UTC :如果你想要一个带有尾随Z UTC 字符串,请使用ZoneOffset.UTC

    ZonedDateTime dateTime = ZonedDateTime.now(ZoneOffset.UTC);

2020-02-14Z 2020-02-14Z

If you have got a ZonedDateTime that is not in UTC, convert:如果您有一个不在 UTC 中的ZonedDateTime ,请转换:

    ZonedDateTime dateTime = ZonedDateTime.now(ZoneId.of("America/Fortaleza"));
    OffsetDateTime odt = dateTime.toOffsetDateTime()
            .withOffsetSameInstant(ZoneOffset.UTC);
    String dateForXml = odt.format(DateTimeFormatter.ISO_OFFSET_DATE);

2020-02-14Z 2020-02-14Z

You should use DateTimeFormatter.ofPattern("yyyy-MM-dd'Z'").您应该使用 DateTimeFormatter.ofPattern("yyyy-MM-dd'Z'")。 Here is what i got:这是我得到的:

LocalDate localDate = LocalDate.now();
ZonedDateTime zonedDateTime = localDate.atStartOfDay(ZoneId.of("EST5EDT"));
System.out.println("Not formatted:" + zonedDateTime);
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'Z'");
System.out.println("Formatted:" + formatter.format(zonedDateTime));

Not formatted:2020-02-14T00:00-05:00[EST5EDT]未格式化:2020-02-14T00:00-05:00[EST5EDT]

Formatted:2020-02-14Z格式:2020-02-14Z

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我需要将 ZonedDateTime 形式的格式“yyyy/MM/dd”更改为“dd/MM/yyyy” - I need to change the format of a ZonedDateTime form "yyyy/MM/dd" to "dd/MM/yyyy" 如何将 localDateTime 转换为格式`dd/MM/yyyy`? - How convert localDateTime to format `dd/MM/yyyy`? 如何将用户以MM-dd-yyyy格式选择的日期转换为Java中的yyyy-MM-dd格式 - How to convert a date a user selects in MM-dd-yyyy format to the format yyyy-MM-dd in Java ZonedDateTime 来自 yyyy-mm-dd 中的日期字符串 - ZonedDateTime from date string in yyyy-mm-dd 如何将 ZonedDateTime 格式化为字符串? - How to format a ZonedDateTime to a String? 如何防止FastDateFormat模式“yyyy-MM-dd”以“dd-MM-yyyy”格式解析String - How to prevent FastDateFormat pattern “yyyy-MM-dd” from parsing String in format “dd-MM-yyyy” 如何将 LocalDate 对象格式化为 MM/dd/yyyy 并保持格式 - How to format LocalDate object to MM/dd/yyyy and have format persist 如何格式化ZonedDateTime - How to format ZonedDateTime 如何将日期和时间格式从“ yyyy-MM-dd HH:mm:ss”更改为“ h:mm a”? - How to change date and time format from“yyyy-MM-dd HH:mm:ss” to “h:mm a”? 如何比较 java 格式“EEE MMM dd HH:mm:ss zzz yyyy”和“yyyy-MM-dd hh:mm:sss”的日期时间? - How to compare the datetime of format "EEE MMM dd HH:mm:ss zzz yyyy" and "yyyy-MM-dd hh:mm:sss" in java?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM