如何解析"YYYY-MM-DD"与乔达时间 [英] How do I parse "YYYY-MM-DD" with joda time

查看:79
本文介绍了如何解析"YYYY-MM-DD"与乔达时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用joda-time来解析格式为YYYY-MM-DD的日期字符串.我有这样的测试代码:

I'm trying to use joda-time to parse a date string of the form YYYY-MM-DD. I have test code like this:

DateTimeFormatter dateDecoder = DateTimeFormat.forPattern("YYYY-MM-DD");
DateTime dateTime = dateDecoder.parseDateTime("2005-07-30");

System.out.println(dateTime);

哪个输出:

2005-01-30T00:00:00.000Z

如您所见,生成的DateTime对象是30 Jan 2005,而不是30 July 2005.

As you can see, the DateTime object produced is 30 Jan 2005, instead of 30 July 2005.

感谢任何帮助.我只是认为这行得通,因为它是此处中列出的日期格式之一.

Appreciate any help. I just assumed this would work because it's one of the date formats listed here.

推荐答案

ISO格式实际上是什么. YYYY-MM-DD不是ISO格式,实际的日期是.

The confusion is with what the ISO format actually is. YYYY-MM-DD is not the ISO format, the actual resulting date is.

因此2005-07-30 ISO-8601格式,并且规范使用YYYY-MM-DD描述格式.在规范中使用YYYY-MM-DD作为模式与任何代码之间都没有联系.规范所受的唯一限制是,结果由4位数字的年份后跟一个破折号,一个2位数字的月份,一个破折号和一个两位数的月份组成.

So 2005-07-30 is in ISO-8601 format, and the spec uses YYYY-MM-DD to describe the format. There is no connection between the use of YYYY-MM-DD as a pattern in the spec and any piece of code. The only constraint the spec places is that the result consists of a 4 digit year folowed by a dash followed by a 2 digit month followed by a dash followed by a two digit day-of-month.

因此,规范可以使用$year4-$month2-$day2,它同样可以定义输出格式.

As such, the spec could have used $year4-$month2-$day2, which would equally well define the output format.

您将需要搜索并替换任何输入模式,才能将"Y"转换为"y",将"D"转换为"d".

You will need to search and replace any input pattern to convert "Y" to "y" and "D" to "d".

我还添加了一些增强的文档格式.

I've also added some enhanced documentation of formatting.

这篇关于如何解析"YYYY-MM-DD"与乔达时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆