Joda DateTime格式无效 [英] Joda DateTime Invalid format

查看:124
本文介绍了Joda DateTime格式无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用DateTimeFormat模式获取当前的DateTime,但是我正在获取异常...

I'm trying to get the current DateTime with my DateTimeFormat pattern, but i'm getting the exception...

//sets the current date
DateTime currentDate = new DateTime();
DateTimeFormatter dtf = DateTimeFormat.forPattern("dd/MM/YYYY HH:mm").withLocale(locale);
DateTime now = dtf.parseDateTime(currentDate.toString());

我遇到了此异常,我无法理解谁给出了格式错误的格式

I'm getting this exception, I cannot understand who is giving the malformed format

java.lang.IllegalArgumentException: Invalid format: "2017-01-04T14:24:17.674+01:00" is malformed at "17-01-04T14:24:17.674+01:00"


推荐答案

此行 DateTime现在= dtf.parseDateTime(currentDate.toString()); 是不正确的,因为您尝试使用默认的toSring格式解析日期。您必须解析格式与模式相同的字符串:

This line DateTime now = dtf.parseDateTime(currentDate.toString()); isn't correct because you try parse date with default toSring format. You have to parse string which formatted the same way as pattern:

DateTime currentDate = new DateTime();
DateTimeFormatter dtf = DateTimeFormat.forPattern("dd/MM/YYYY HH:mm").withLocale(locale);
String formatedDate = dtf.print(currentDate);
System.out.println(formatedDate);
DateTime now = dtf.parseDateTime(formatedDate);
System.out.println(now);

这篇关于Joda DateTime格式无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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