Java:不可稀释的日期异常 [英] Java: unparseable date exception
问题描述
在尝试转换日期格式时,我收到一个异常:不可稀释的日期,不知道如何解决这个问题。
我收到一个代表事件日期的字符串,并希望在GUI中以不同的格式显示此日期。
我想要做的是:
private String modifyDateLayout(String inputDate){
try {
// inputDate =2010-01-04 01:32:27 UTC;
Date date = new SimpleDateFormat(yyyy-MM-dd HH:mm:ss z)。parse(inputDate);
返回新的SimpleDateFormat(dd.MM.yyyy HH:mm:ss)。format(date);
} catch(ParseException e){
e.printStackTrace();
return15.01.2010;
}
}
无论如何,
String modifiedDateString = originalDate.toString();
是虚拟的。我想以以下格式获取日期字符串:
dd.MM.yyyy HH:mm:ss
,输入String示例如下:
2010 01-04 01:32:27 UTC
有人知道如何将上面的示例date(String)转换成String格式dd.MM.yyyy HH:mm:ss?
谢谢!
编辑:我修正了错误输入日期格式但仍然不起作用。以上是粘贴方法,下面是调试会话中的屏幕图像。
alt text http://img683.imageshack.us/img683/193/dateproblem.png
#Update
我跑了
String [] timezones = TimeZone.getAvailableIDs();
,数组中有UTC字符串。这是一个奇怪的问题。
我做了一个肮脏的黑客工程:
private String modifyDateLayout (String inputDate){
try {
inputDate = inputDate.replace(UTC,);
Date date = new SimpleDateFormat(yyyy-MM-dd HH:mm:ss)。parse(inputDate);
返回新的SimpleDateFormat(dd.MM.yyyy HH:mm:ss)。format(date);
} catch(ParseException e){
e.printStackTrace();
return15.01.2010;
}
}
但是我仍然希望将原始输入转换为没有切割时区。
此代码是使用JDK 1.6为Android手机编写的。
你在这里基本做的是依靠 Date#toString()
已经有一个固定的模式。要将Java Date
对象转换为另一种可读的String模式,您需要 SimpleDateFormat#format()
。 p>
private String modifyDateLayout(String inputDate)throws ParseException {
Date date = new SimpleDateFormat(yyyy-MM-dd HH: mm:ss z)。parse(inputDate);
返回新的SimpleDateFormat(dd.MM.yyyy HH:mm:ss)。format(date);
}
顺便说一下,不可稀疏日期异常可以在这里 被 SimpleDateFormat#parse()
。这意味着 inputDate
不在预期的模式yyyy-MM-dd HH:mm:ss z
。您可能需要修改模式以匹配 inputDate
的实际模式。
更新:好的,我做了一个测试:
public static void main(String [] args)throws Exception {
String inputDate =2010-01-04 01:32:27 UTC;
String newDate = new Test()。modifyDateLayout(inputDate);
System.out.println(newDate);
}
正确打印:
03.01.2010 21:32:27
我在GMT-4)
更新2:根据你的编辑,你真的有一个 While trying to transform the date format I get an exception:unparseable date and don't know how to fix this problem. I am receiving a string which represents an event date and would like to display this date in different format in GUI. What I was trying to do is the following: Anyway the line is dummy. I would like to get a date string in the following format: dd.MM.yyyy HH:mm:ss and the input String example is the following: 2010-01-04 01:32:27 UTC Does anyone know how to convert the example date (String) above into a String format dd.MM.yyyy HH:mm:ss? Thank you! Edit: I fixed the wrong input date format but still it doesn't work. Above is the pasted method and below is the screen image from debugging session. alt text http://img683.imageshack.us/img683/193/dateproblem.png #Update
I ran and there is UTC String in the array. It's a strange problem. I did a dirty hack that works: But still I would prefer to transform the original input without cutting timezone away. This code is written for Android phone using JDK 1.6. What you're basically doing here is relying on By the way, the "unparseable date" exception can here only be thrown by Update: Okay, I did a test: This correctly prints: (I'm on GMT-4) Update 2: as per your edit, you really got a 这篇关于Java:不可稀释的日期异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋! ParseException
。那么最可疑的部分将是 UTC
的时区。这在Java环境中是否真的被知道?你使用什么Java版本和什么操作系统版本?检查 TimeZone.getAvailableIDs()
。 private String modifyDateLayout(String inputDate){
try {
//inputDate = "2010-01-04 01:32:27 UTC";
Date date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z").parse(inputDate);
return new SimpleDateFormat("dd.MM.yyyy HH:mm:ss").format(date);
} catch (ParseException e) {
e.printStackTrace();
return "15.01.2010";
}
}
String modifiedDateString = originalDate.toString();
String[] timezones = TimeZone.getAvailableIDs();
private String modifyDateLayout(String inputDate){
try {
inputDate = inputDate.replace(" UTC", "");
Date date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(inputDate);
return new SimpleDateFormat("dd.MM.yyyy HH:mm:ss").format(date);
} catch (ParseException e) {
e.printStackTrace();
return "15.01.2010";
}
}
Date#toString()
which already has a fixed pattern. To convert a Java Date
object into another human readable String pattern, you need SimpleDateFormat#format()
.private String modifyDateLayout(String inputDate) throws ParseException{
Date date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z").parse(inputDate);
return new SimpleDateFormat("dd.MM.yyyy HH:mm:ss").format(date);
}
SimpleDateFormat#parse()
. This means that the inputDate
isn't in the expected pattern "yyyy-MM-dd HH:mm:ss z"
. You'll probably need to modify the pattern to match the inputDate
's actual pattern.public static void main(String[] args) throws Exception {
String inputDate = "2010-01-04 01:32:27 UTC";
String newDate = new Test().modifyDateLayout(inputDate);
System.out.println(newDate);
}
03.01.2010 21:32:27
ParseException
on that. The most suspicious part would then be the timezone of UTC
. Is this actually known at your Java environment? What Java version and what OS version are you using? Check TimeZone.getAvailableIDs()
. There must be a UTC
in between.