java 我的 Joda 时间格式模式在解析的 DateTime 输出中生成“T”和“Z”是否不正确?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4675241/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 07:28:27  来源:igfitidea点击:

Is my Joda Time format pattern incorrect to produce a `T` and `Z` inside the parsed DateTime output?

javajodatimedate-format

提问by Tree

Using Joda Time's pattern syntaxbelow, this input string:

使用以下Joda Time 的模式语法,此输入字符串:

Sunday, January 09, 2011 6:15:00 PM

becomes this datetime:

成为这个日期时间:

2011-01-09T06:15:00.000Z

Code:

代码:

String start = "Sunday, January 09, 2011 6:15:00 PM";

DateTimeFormatter parser1 = 
DateTimeFormat.forPattern("EEEE, MMMM dd, yyyy H:mm:ss aa");

DateTime startTime = parser1.parseDateTime(start);

Is this format pattern incorrect? If not, what are the Tand Zdoing inside the DateTime output?

这种格式模式不正确吗?如果不是,DateTime 输出中的TZ做什么?

2011-01-09T06:15:00.000Z

回答by cheekoo

T: Denotes start of "time part" of the string. Zone: 'Z' outputs offset. I suppose in thise case is GMT. Source:http://joda-time.sourceforge.net/apidocs/org/joda/time/format/DateTimeFormat.html

T:表示字符串“时间部分”的开始。区域:'Z' 输出偏移。我想在这种情况下是格林威治标准时间。来源:http: //joda-time.sourceforge.net/apidocs/org/joda/time/format/DateTimeFormat.html

I always use this format string: yyyy-MM-dd'T'HH:mm:ss.SSSZ

我总是使用这种格式字符串: yyyy-MM-dd'T'HH:mm:ss.SSSZ

And, yes, they are not incorrect, if they are present in your string.

而且,是的,如果它们出现在您的字符串中,它们并没有错。

回答by Jon Skeet

You've only shown the parsingcode - not how you've converted the DateTimevalue back to a String.

您只显示了解析代码 - 而不是您如何将DateTime值转换回String.

I strongly suspect you're just calling toString(), which use the default DateTimeISO8601 format.

我强烈怀疑您只是在调用toString(),它使用默认的DateTimeISO8601 格式。

Don't forget that a DateTimevalue represents an instant in time in a particular time zone and calendar system - it has no concept of format patterns. The "T" and "Z" aren't in the DateTimevalue - they're just in the default representationof the value. It's like when you convert an intto a string - it happens to use decimal, but the number itself is just a number.

不要忘记一个DateTime值代表特定时区和日历系统中的一个瞬间——它没有格式模式的概念。“T”和“Z”不在DateTime值中——它们只是在值的默认表示中。就像将 an 转换int为字符串一样 - 它恰好使用十进制,但数字本身只是一个数字。

If you want to format a DateTimein a specific way, you should use

如果要以DateTime特定方式格式化 a ,则应使用

String text = formatter.print(startTime);

回答by Carlos Vilchez

Instead of creating your own formatter, you can use one predefined by Joda time. You will find them in org.joda.time.format.ISODateTimeFormat.

您可以使用 Joda 时间预定义的格式化程序,而不是创建自己的格式化程序。您会在org.joda.time.format.ISODateTimeFormat.

In fact, in your case I would use ISODateTimeFormat.basicDateTimeNoMillisformatter. I think it has the pattern you were looking for: yyyyMMdd'T'HHmmssZ

事实上,在你的情况下,我会使用ISODateTimeFormat.basicDateTimeNoMillis格式化程序。我认为它具有您正在寻找的模式:yyyyMMdd'T'HHmmssZ