问题描述
格式化程序在解析一个DateTime类的方法必须匹配字符串的确切样式?例如,我从数据库(Oracle)获取一个TimeStamp对象并将其转换为一个字符串。在数据库中,TimeStamp存储如下
Does the style of the formatter in the parse method of the DateTime class have to match the exact style of the string? For instance, I'm getting a TimeStamp object from the database (Oracle) and converting it to a string. In the database the TimeStamp is stored like this
我将格式化器设置为此样式
I set my formatter to this style
String pattern = "dd-MMM-yy";
我得到这个例外
java.lang.IllegalArgumentException: Invalid format: "08-AUG-12 12.00.00 AM" is malformed at " 12.00.00 AM"
org.joda.time.format.DateTimeFormatter.parseDateTime(DateTimeFormatter.java:866)
org.joda.time.DateTime.parse(DateTime.java:144)
这是什么意思,我该如何解决?当我将格式化程序设置为yy-MMM-dd hh.mm.ss aa
我没有得到例外,但它在浏览器中打印如下: 2008-08-12T00:00:00.000-04:00
,但是我需要它作为打印出来dd-MMM-yy hh:mm: ss aa
What exactly does this mean and how would I go about fixing it? When I set my formatter to "yy-MMM-dd hh.mm.ss aa"
I don't get an exception but it prints in the browser like this: 2008-08-12T00:00:00.000-04:00
, but I need for it to print out as "dd-MMM-yy hh:mm:ss aa"
推荐答案
使用LocalDateTime:
Use LocalDateTime instead:
String input = "08-AUG-12 12.00.00 AM";
String pattern = "dd-MMM-yy hh.mm.ss aa";
LocalDateTime localDateTime = LocalDateTime.parse(input, DateTimeFormat.forPattern(pattern));
编辑
事实上你可以使用DateTime来执行它:
As a matter of fact you can do it with DateTime also:
private static String parseDateTime(String input){
String pattern = "dd-MMM-yy hh.mm.ss aa";
DateTime dateTime = DateTime.parse(input, DateTimeFormat.forPattern(pattern));
return dateTime.toString("dd-MMM-yy hh:mm:ss aa");
}
这篇关于了解JodaTime DateTime.parse(string,formatter)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!