如何将ZonedDateTime格式化为字符串?
- 论坛
- 如何将ZonedDateTime格式化为字符串?
26 浏览
如何将ZonedDateTime格式化为字符串?
我想将一个ZonedDateTime
对象转换为String
,格式为("dd/MM/yyyy - hh:mm")
。我知道在Joda-Time的其他类型中,可以使用toString("dd/MM/yyyy - hh:mm")
来实现。但是这对于ZonedDateTime.toString()
无效。
如何格式化ZonedDateTime
为String
?
编辑:
我尝试在另一个时区打印时间,结果似乎总是相同的:
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)); // 与上一个结果相同 // 24/02/2017 - 04:53 System.out.println(DateTimeFormatter.ofPattern("dd/MM/yyyy - hh:mm").format(date2));
而我并不在洛杉矶的时区。
编辑2:
找到了如何更改时区:
// 更改为: ZonedDateTime date2 = date.withZoneSameInstant(la);