我正在使用 Joda API 来格式化当前时间(结果必须是格式为“yyyy-MM-dd HH:mm:ss”的字符串)。下面我提供了我的代码和错误消息:
DateTimeFormatter dtf = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss");
DateTime dt = new DateTime();
String datetime = dtf.parseDateTime(dt.toString()).toString();
错误信息:
Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: Invalid format: "2014-11-17T11:47:29.229+01:00" is malformed at "T11:47:29.229+01:00" at org.joda.time.format.DateTimeFormatter.parseDateTime(DateTimeFormatter.java:899)
最佳答案
如果你想使用自定义格式转换为字符串
DateTimeFormatter dtf = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss");
DateTime dt = new DateTime();
String datetime = dtf.print(dt);
你当前在最后一行所做的是
String defaultFormatted = dt.toString();
// this is what contains the "T11:47:29.229+01:00" part
DateTime dateTime = dtf.parseDateTime(defaultFormatted);
// we're back at the beginning, this is equivalent to your original "dt"
String defaultFormattedAgain = dateTime.toString();
// and this is the same as the initial string with T11:..
因此,您多次从字符串转换为 &,但从未使用 dtf
来实际格式化字符串的外观。
关于java - Joda Time : Invalid format,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26971035/