2013-03-29 156 views
0

將長日期格式轉換爲特定的短日期格式。將長日期轉換爲特定的短日期格式

我想從Datepicker(Jcalander)得到Date,格式爲dd-mm-yyyy格式並賦值給String變量。我嘗試使用下面顯示的代碼。但沒有得到我想要的日期格式。

SimpleDateFormat simpleFormat = (SimpleDateFormat) jCalendarCombo1.getDateFormat(); 
Date date = jCalendarCombo1.getDate(); 
System.out.println(date); // Prints Thu Mar 28 00:00:00 IST 2013 


String s = simpleFormat.format(date); 
System.out.println(s); // prints Thursday, March 28, 2013 

System.out.println("Date SHORT format: " + DateFormat.getDateInstance(DateFormat.SHORT).format(date)); // prints 3/28/13 

回答

0

如果你想有一個固定,非語言環境相關格式,你可以自己創建它;

SimpleDateFormat shortformat = new SimpleDateFormat("dd-MM-yyyy"); 
String s = shortformat.format(date); 
System.out.println(s);      // Prints 29-03-2013 
+0

Thnx爲您的快速反應。它現在有效。我以2013年3月29日的格式獲得了日期。 thnx再次。 – amal