简体   繁体   English

如何将 ZonedDateTime 格式化为字符串?

[英]How to format a ZonedDateTime to a String?

I want to convert a ZonedDateTime to a String in the format of ("dd/MM/yyyy - hh:mm") .我想将ZonedDateTime转换为("dd/MM/yyyy - hh:mm")格式的String I know this is possible in Joda-Time other types, just using their toString("dd/MM/yyyy - hh:mm") ....But this doesn't work with ZonedDateTime.toString() .我知道这在 Joda-Time 其他类型中是可能的,只需使用它们的toString("dd/MM/yyyy - hh:mm") ....但这不适用于ZonedDateTime.toString()

How can I format a ZonedDateTime to a String ?如何将ZonedDateTime格式化为String


EDIT:编辑:

I tried to print the time in another timezone and the result appears to be the same always:我试图在另一个时区打印时间,结果似乎总是一样的:

ZonedDateTime date = ZonedDateTime.now();
ZoneId la = ZoneId.of("America/Los_Angeles");
ZonedDateTime date2 = date.of(date.toLocalDateTime(), la);

// 24/02/2017 - 04:53
System.out.println(DateTimeFormatter.ofPattern("dd/MM/yyyy - hh:mm").format(date));
// same result as the previous one
// 24/02/2017 - 04:53
System.out.println(DateTimeFormatter.ofPattern("dd/MM/yyyy - hh:mm").format(date2));

And I am not in the same timezone as Los Angeles.而且我和洛杉矶不在同一个时区。


EDIT 2:编辑 2:

Found how to change the timezones:找到了如何更改时区:

// Change this:
ZonedDateTime date2 = date.of(date.toLocalDateTime(), la); // incorrect!
// To this:
ZonedDateTime date2 = date.withZoneSameInstant(la);

You can use java.time.format.DateTimeFormatter. 您可以使用java.time.format.DateTimeFormatter。 https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html

Here there is an example 这里有一个例子

ZonedDateTime date = ZonedDateTime.now();

System.out.println(DateTimeFormatter.ofPattern("dd/MM/yyyy - hh:mm").format(date));

The format dd/MM/yyyy - hh:mm does not make sense格式dd/MM/yyyy - hh:mm没有意义

The format dd/MM/yyyy - hh:mm does not make sense because of missing a which gives it a meaning.格式dd/MM/yyyy - hh:mm没有意义,因为缺少a它意义的 a。 Note that h represents clock-hour-of-am-pm (1-12) and a represents am-pm-of-day ie without a , the hour returned by h will be ambiguous eg without a , h will not differentiate between 9:00 am and 9:00 pm.请注意, h代表clock-hour-of-am-pm (1-12)a代表am-pm-of-day即没有a , h 返回的h将不明确,例如没有ah不会区分 9上午 :00 和晚上 9:00。

Therefore, use H which represents hour-of-day (0-23) or use h with a .因此,使用代表一天中的小时 (0-23) 的H或将ha一起使用

Also, always use Locale with the formatter because Date-Time parsing/formatting API is Locale-sensitive .此外,始终将Locale与格式化程序一起使用,因为日期时间解析/格式化 API 是 Locale-sensitive

ZoneId zoneId = ZoneId.of("Europe/London");
ZonedDateTime zdt9Am = ZonedDateTime.of(LocalDate.now(), LocalTime.of(9, 0), zoneId);
ZonedDateTime zdt9Pm = ZonedDateTime.of(LocalDate.now(), LocalTime.of(21, 0), zoneId);

DateTimeFormatter ambiguousFormatter = 
                    DateTimeFormatter.ofPattern("dd/MM/yyyy - hh:mm", Locale.ENGLISH);
String ambiguosDtStr1 = zdt9Am.format(ambiguousFormatter);
String ambiguosDtStr2 = zdt9Pm.format(ambiguousFormatter);
System.out.println(ambiguosDtStr1); // 08/10/2022 - 09:00
System.out.println(ambiguosDtStr2); // 08/10/2022 - 09:00

DateTimeFormatter correctAmPmFormatter = 
                    DateTimeFormatter.ofPattern("dd/MM/yyyy - hh:mm a", Locale.ENGLISH);
System.out.println(zdt9Am.format(correctAmPmFormatter)); // 08/10/2022 - 09:00 AM
System.out.println(zdt9Pm.format(correctAmPmFormatter)); // 08/10/2022 - 09:00 PM

DateTimeFormatter correct24HourFormatter = 
                    DateTimeFormatter.ofPattern("dd/MM/yyyy - HH:mm", Locale.ENGLISH);
System.out.println(zdt9Am.format(correct24HourFormatter)); // 08/10/2022 - 09:00
System.out.println(zdt9Pm.format(correct24HourFormatter)); // 08/10/2022 - 21:00

Check DateTimeFormatter documentation to learn more about it.查看DateTimeFormatter文档以了解更多信息。

Learn more about the modern Date-Time API from Trail: Date Time .Trail:Date Time了解有关现代日期时间 API的更多信息。

Many thanks for above. 非常感谢上面。 Here it is in scala where localDateTime.now always Zulu/UTC time. 这里是scala,其中localDateTime.now总是Zulu / UTC时间。

import java.time.format.DateTimeFormatter
import java.time.LocalDateTime
import java.time.ZoneId

val ny = ZoneId.of("America/New_York")
val utc = ZoneId.of("UTC")
val dateTime = LocalDateTime.now.atZone(utc)

val nyTime = DateTimeFormatter.
      ofPattern("yyyy-MMM-dd HH:mm z").
      format(dateTime.withZoneSameInstant(ny))

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM