在Joda

编程入门 行业动态 更新时间:2024-10-23 15:22:28
Joda-Time中,将日期时间分别为日期和时间,时区为“Z”(Separate DateTime to Date and Time with time zone `Z` at the end, in Joda-Time)

我有一个Joda-Time DateTime对象,需要分别设置日期和时间,最后是时区标签:

DateTime dateTime = new DateTime(); System.out.println(dateTime.toString("YYYY-MM-ddZ")); System.out.println(dateTime.toString("HH:mm:ssZ"));

在这种情况下,输出将是:

2014-02-27+0000 15:10:36+0000

几乎就是我需要的东西,但它有可能像这样吗?

2014-02-27Z 15:10:36Z

I have a Joda-Time DateTime object and need to have date and time separately, with time zone label at the end:

DateTime dateTime = new DateTime(); System.out.println(dateTime.toString("YYYY-MM-ddZ")); System.out.println(dateTime.toString("HH:mm:ssZ"));

In this case the output will be:

2014-02-27+0000 15:10:36+0000

Almost exactly what I need, but it is possible to have it like this?

2014-02-27Z 15:10:36Z

最满意答案

这是一个有效的例子

DateTime dateTime = new DateTime(); DateTimeFormatter formatter = new DateTimeFormatterBuilder() .appendPattern("yyyy-MM-dd") .appendTimeZoneOffset("Z", false, 2, 2) .toFormatter(); System.out.println(formatter.print(dateTime.withZone(DateTimeZone .forID("Zulu"))));

基本上,如果时区偏移为零,则打印Z 由于Zulu或UTC的偏移量为0,因此将打印出来。

Here's a working example

DateTime dateTime = new DateTime(); DateTimeFormatter formatter = new DateTimeFormatterBuilder() .appendPattern("yyyy-MM-dd") .appendTimeZoneOffset("Z", false, 2, 2) .toFormatter(); System.out.println(formatter.print(dateTime.withZone(DateTimeZone .forID("Zulu"))));

Basically, if the time zone offset is zero, you print Z. Since Zulu or UTC has an offset of 0, that's what will be printed.

更多推荐

本文发布于:2023-07-26 15:38:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1277434.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Joda

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!