Java Joda DateTime ISODateTimeFormat 模式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23987332/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Joda DateTime ISODateTimeFormat pattern
提问by Dave Pile
The Joda ISODateTimeFormatdocs say ISODateTimeFormat.dateTime()
returns a formatter for the pattern yyyy-MM-dd'T'HH:mm:ss.SSSZZ
Joda ISODateTimeFormat文档说ISODateTimeFormat.dateTime()
返回模式yyyy-MM-dd'T'HH:mm:ss.SSSZZ 的格式化程序
But the formatter returns a "Z" in place of +00:00
see this-
但是格式化程序返回一个“Z”代替 +00:00
看到这个 -
DateTime dt = DateTime.now(DateTimeZone.UTC);
DateTimeFormatter patternFormat = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZZ");
DateTimeFormatter isoFormat = ISODateTimeFormat.dateTime();
System.out.println(dt.toString(patternFormat)); //2014-06-01T03:02:13.552+00:00
System.out.println(dt.toString(isoFormat)); //2014-06-01T03:02:13.552Z
Can anyone tell me what the pattern would be to get the +00:00 to print as a Z
谁能告诉我将 +00:00 打印为 Z 的模式是什么
Edit: Just to clarify- I know that the 'Z' is the same as +00:00 but textually it is different. What I am asking is what pattern would place a Z as the time offset instead of +00:00
编辑:只是为了澄清 - 我知道“Z”与 +00:00 相同,但在文本上是不同的。我要问的是什么模式会将 Z 作为时间偏移而不是 +00:00
(Sorry if this is too trivial. I wanted to use the ISO format without milliseconds and in the course of writing this question I found exactly what I was after in ISODateTimeFormat.dateTimeNoMillis()
so I am asking now only for interests sake)
(对不起,如果这太琐碎了。我想在没有毫秒的情况下使用 ISO 格式,在编写这个问题的过程中,我找到了我所追求的内容,ISODateTimeFormat.dateTimeNoMillis()
所以我现在只是为了利益而询问)
采纳答案by araqnid
It doesn't appear that you can build such a formatter purely from a pattern. The DateTimeFormat doc says:
您似乎无法完全从模式中构建这样的格式化程序。DateTimeFormat 文档说:
Zone:
- 'Z' outputs offset without a colon,
- 'ZZ' outputs the offset with a colon,
- 'ZZZ' or more outputs the zone id.
区域:
- 'Z' 输出没有冒号的偏移量,
- 'ZZ' 用冒号输出偏移量,
- 'ZZZ' 或更多输出区域 ID。
You can build most of the formatter from a pattern and then customise the time zone output like this:
您可以从模式构建大部分格式化程序,然后像这样自定义时区输出:
DateTimeFormatter patternFormat = new DateTimeFormatterBuilder()
.appendPattern("yyyy-MM-dd'T'HH:mm:ss.SSS")
.appendTimeZoneOffset("Z", true, 2, 4)
.toFormatter();
回答by Wundwin Born
But the formatter returns a "Z" in place of +00:00 see this-
See doc again, it said clearly,
再看医生,说的很清楚,
The time zone offset is 'Z' for zero, and of the form '±HH:mm' for non-zero.
So this ISO value 2014-06-01T03:02:13.552Zis equivalent to 2014-06-01T03:02:13.552+00:00.
所以这个 ISO 值2014-06-01T03:02:13.552Z相当于2014-06-01T03:02:13.552+00:00。
In your code to see non-zero case, try with
在您的代码中查看非零情况,请尝试使用
DateTime dt = DateTime.now(); //without arg DateTimeZone.UTC;
回答by Dileep
'Z'
is the short term for Zulu time
, which is the same as GMT
or UTC
'Z'
是 的短期Zulu time
,它与GMT
或UTC
I think this is what you need ..!!
我认为这就是你所需要的..!!
int offset = DateTimeZone.forID("UTC").getOffset(new DateTime());
回答by pacifier21
If you know that your TimeZone will always be DateTimeZone.UTC, then you can inject the Z in the pattern just like the T character has been injected. Something like this:
如果您知道您的 TimeZone 将始终是 DateTimeZone.UTC,那么您可以在模式中注入 Z,就像注入 T 字符一样。像这样的东西:
DateTimeFormatter patternFormat = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
If you want to have the same format but without the milliseconds, the pattern might look something like this:
如果您想使用相同的格式但没有毫秒,则模式可能如下所示:
DateTimeFormatter patternFormat = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss'Z'");
I am sure you have found this page, but see the Joda Time Formattingreference page for details regarding these formats and other options.
我相信你已经找到了这个页面,但请参阅Joda 时间格式参考页面以了解有关这些格式和其他选项的详细信息。