問題描述
在嘗試轉換日期格式時出現異常:無法解析的日期并且不知道如何解決此問題.
While trying to transform the date format I get an exception:unparseable date and don't know how to fix this problem.
我收到一個代表事件日期的字符串,并希望在 GUI 中以不同的格式顯示該日期.
I am receiving a string which represents an event date and would like to display this date in different format in 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);
return new SimpleDateFormat("dd.MM.yyyy HH:mm:ss").format(date);
} catch (ParseException e) {
e.printStackTrace();
return "15.01.2010";
}
}
不管怎樣
String modifiedDateString = originalDate.toString();
是假的.我想獲取以下格式的日期字符串:
is dummy. I would like to get a date string in the following format:
dd.MM.yyyy HH:mm:ss
dd.MM.yyyy HH:mm:ss
輸入字符串示例如下:
2010-01-04 01:32:27 UTC
2010-01-04 01:32:27 UTC
有誰知道如何將上面的示例日期(字符串)轉換為字符串格式dd.MM.yyyy HH:mm:ss?
Does anyone know how to convert the example date (String) above into a String format dd.MM.yyyy HH:mm:ss?
謝謝!
我修復了錯誤的輸入日期格式,但仍然無法正常工作.上面是粘貼的方法,下面是調試會話的屏幕圖像.
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.
替代文字 http://img683.imageshack.us/img683/193/dateproblem.png
#更新我跑了
String[] timezones = TimeZone.getAvailableIDs();
并且數組中有UTC字符串.這是一個奇怪的問題.
and there is UTC String in the array. It's a strange problem.
我做了一個有效的骯臟黑客:
I did a dirty hack that works:
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";
}
}
但我仍然希望在不削減時區的情況下轉換原始輸入.
But still I would prefer to transform the original input without cutting timezone away.
此代碼是為使用 JDK 1.6 的 Android 手機編寫的.
This code is written for Android phone using JDK 1.6.
推薦答案
你在這里基本上做的是依賴 Date#toString()
已經有一個固定的模式.要將 Java Date
對象轉換為另一種人類可讀的字符串模式,您需要 SimpleDateFormat#format()
.
What you're basically doing here is relying on 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()
.這意味著 inputDate
不在預期的模式 "yyyy-MM-dd HH:mm:ss z"
中.您可能需要修改模式以匹配 inputDate
的實際模式.
By the way, the "unparseable date" exception can here only be thrown by 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.
更新:好的,我做了一個測試:
Update: Okay, I did a test:
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: 根據您的編輯,您確實得到了一個 ParseException
.最可疑的部分將是 UTC
的時區.這在您的 Java 環境中實際上是已知嗎?您使用的是什么 Java 版本和什么操作系統版本?檢查 TimeZone.getAvailableIDs()
.中間必須有一個UTC
.
Update 2: as per your edit, you really got a 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.
這篇關于Java:不可解析的日期異常的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!