我想将一个ZonedDateTime
转换成格式为("dd/MM/yyyy - hh:mm")
的String
。我知道在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));
// same result as the previous one
// 24/02/2017 - 04:53
System.out.println(DateTimeFormatter.ofPattern("dd/MM/yyyy - hh:mm").format(date2));
我所在的时区不同于洛杉矶。
编辑2:
找到了如何更改时区:
// Change this:
ZonedDateTime date2 = date.of(date.toLocalDateTime(), la); // incorrect!
// To this:
ZonedDateTime date2 = date.withZoneSameInstant(la);