[英]Java - Convert string to ISO 8601 date format error Joda-Time
I am trying to convert the date entered by my app user as String to ISO 8601 format using Joda-Time , so I am using the following code but I get error: 我正在尝试使用Joda-Time将应用程序用户以String形式输入的日期转换为ISO 8601格式,因此我正在使用以下代码,但出现错误:
String oldDate = "05/05/2013";
DateTime oldD = DateTime.parse(oldDate);
DateTimeFormatter OldDFmt = ISODateTimeFormat.dateTime();
String str = OldDFmt.print(oldD);
System.out.println(str);
but I am always getting error: 但我总是出错:
org.springframework.web.util.NestedServletException: Request processing failed;
org.springframework.web.util.NestedServletException:请求处理失败; nested exception is java.lang.IllegalArgumentException: Invalid format: "05/05/2013" is malformed at "/05/2013"
嵌套的异常为java.lang.IllegalArgumentException:无效的格式:“ 05/05/2013”的格式为“ / 05/2013”
Can someone please help and tell me what I am doing wrong here? 有人可以帮忙告诉我我在做什么错吗? Thanks.
谢谢。
Can someone please help and tell me what I am doing wrong here?
有人可以帮忙告诉我我在做什么错吗?
The error occurs here. 错误发生在这里。
DateTime oldD = DateTime.parse(oldDate);
The DateTime.parse(String)
method parses your String
argument using a ISODateTimeFormat.dateTimeParser()
. DateTime.parse(String)
方法使用ISODateTimeFormat.dateTimeParser()
解析String
参数。
The format must be of the form 格式必须为以下格式
datetime = time | date-opt-time
time = 'T' time-element [offset]
date-opt-time = date-element ['T' [time-element] [offset]]
date-element = std-date-element | ord-date-element | week-date-element
std-date-element = yyyy ['-' MM ['-' dd]]
ord-date-element = yyyy ['-' DDD]
week-date-element = xxxx '-W' ww ['-' e]
time-element = HH [minute-element] | [fraction]
minute-element = ':' mm [second-element] | [fraction]
second-element = ':' ss [fraction]
fraction = ('.' | ',') digit+
offset = 'Z' | (('+' | '-') HH [':' mm [':' ss [('.' | ',') SSS]]])
which yours 你的
String oldDate = "05/05/2013";
isn't. 不是。
You'll need to parse your String
date with a parser with a corresponding format, like what Paul Hicks is suggesting. 您需要使用解析器来解析
String
日期,解析器具有相应的格式,如Paul Hicks所建议的那样。 You can then format the created DateTime
into the ISO standard format. 然后,您可以将创建的
DateTime
格式化为ISO标准格式。
The formatter you're using to parse the string DateTime.parse(oldDate)
is the default one: according to the javadocs, ISODateTimeFormat.dateTimeParser()
. 您用来解析字符串
DateTime.parse(oldDate)
的格式化程序是默认格式器:根据javadocs ISODateTimeFormat.dateTimeParser()
。 You want a parsing DateTimeFormatter
that knows about your string format dd/mm/yyyy
. 您需要一个解析
DateTimeFormatter
,它知道您的字符串格式dd/mm/yyyy
。 I don't use joda-time, but I think you want this: 我不使用joda-time,但我认为您需要这样做:
DateTimeFormatter stringParser = DateTimeFormat.forPattern("dd/MM/yyyy");
The rest of the code looks ok. 其余代码看起来还不错。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.