如何可以解析,如「2012-08-22 11點55分38秒格式日期的SimpleDateFormat
以 '八一' 的8
我formateador試過=新的SimpleDateFormat(「d MMM日期「);
但我收到'Unparseable date'異常。
希望你能幫助我。謝謝
如何可以解析,如「2012-08-22 11點55分38秒格式日期的SimpleDateFormat
以 '八一' 的8
我formateador試過=新的SimpleDateFormat(「d MMM日期「);
但我收到'Unparseable date'異常。
希望你能幫助我。謝謝
你解析從字符串到Date
。然後,您可以將格式從Date
更改爲使用不同模式的字符串。目前,考慮到你向我們展示的代碼,看起來你試圖解析值「2012-08-22 11:55:38」與SimpleDateFormat
期待模式「d MMM」,這很清楚不符合。你應該有這樣的事情:
SimpleDateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss",
Locale.US);
// TODO: Set the time zone appropriately
Date date = inputFormat.parse(inputText);
SimpleDateFormat outputFormat = new SimpleDateFormat("d MMM", targetLocale);
// TODO: Set the time zone appropriately
String outputText = outputFormat.format(date);
請注意,如果你正在做在Java中的日期/時間的任何工作顯著金額,你應該看看Joda Time,這是一個更好的日期/時間API。
感謝這個 – madprops
這應該工作(如果我理解你的要求):
String s = "2012-08-22 11:55:38";
DateFormat fmt = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
Date dt = fmt.parse(s);
DateFormat output = new SimpleDateFormat("d 'of' MMMM", Locale.US);
System.out.println(output.format(dt)); //prints 22 of August
你有問題,同時分析來自輸入字符串的日期。
「2012-08-22 11:55:38」是「yyyy-MM-dd HH:mm:ss」格式,您應該首先使用它來解析輸入字符串中的日期。
然後,一旦你有一個Date
對象,你可以格式化所需格式的日期。
如果我的問題得到解答,請嘗試將第一個字符串解析爲日期,然後將結果日期格式化爲秒字符串。如果這是真的,那麼看看下面的代碼:
SimpleDateFormat f1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
SimpleDateFormat f2 = new SimpleDateFormat("dd 'of' MMMM");
try {
java.util.Date date = f1.parse("2012-08-22 11:55:38");
System.out.println(f2.format(date));
} catch (ParseException ex) {
ex.printStackTrace();
}
嘗試重組你的問題,我不認爲任何人都可以利用它任何意義 –