我正在開發一個Spring項目,我必須按上傳日期搜索文檔。所以當我把我的日期作爲DAO層中的一個方法的參數傳遞時,它的收到像是:Thu Jun 06 00:03:49 WEST 2013
。我想格式化到:2013-06-06
如何格式化java日期
我已經使用這個代碼,要做到這一點,但它返回06/06/13
和日期格式的其他常量(如DateFormat.MEDIUM,...),不回我所期待的。
DateFormat shortDf = DateFormat.getDateInstance(DateFormat.SHORT);
System.out.println(shortDf.format(new Date())); // return 06/06/13 it's short
我也試着像那樣的SimpleDateFormat:
public static Date parseDate(String date, String format)throws ParseException {
SimpleDateFormat formatter = new SimpleDateFormat(format,Locale.ENGLISH);
return formatter.parse(date);
}
但它仍然拋出一個分析異常:
java.text.ParseException: Unparseable date: "Thu Jun 06 00:23:33 WEST 2013"
at java.text.DateFormat.parse(DateFormat.java:337)
at TestApp.main(TestApp.java:20)
是的,我已經找到這個解決方案,就像你所描述的 –
pPlease如果你覺得它回答你的問題,請將此線程標記爲已回答,以便人們知道在此線程中找到了一個解決方案:) –