2012-04-25 54 views
0

我知道SimpleDateFormat是如何工作的,通過從系統中獲取今天的日期sdfDateTime.format(new Date(System.currentTimeMillis()));。 SimpleDateFormat可以通過抓取字符串來格式化日期嗎?假設您有一個字符串:String dateStr = "04/05/2012";您將如何將其格式化爲:「2012年4月5日」?SimpleDateFormat可以基於字符串格式化日期嗎?

回答

1
DateFormat inputFormat = new SimpleDateFormat("MM/dd/yyyy"); 
inputFormat.setLenient(false); 
DateFormat outputFormat = new SimpleDateFormat("MMMM dd, yyyy"); 
outputFormat.setLenient(false); 

String inputDateAsString = "04/05/2012"; 
Date inputDate = inputFormat.parse(inputDateAsString); 
System.out.println(outputFormat.format(inputDate)); 

你不能只抓住一個任意字符串,並找出它的格式是什麼。

+0

不錯!正是我正在尋找的 - 謝謝duffymo! – kirktoon1882 2012-04-25 20:40:28