2011-03-23 22 views
6

在Java中將23-Mar-2011轉換爲23-03-2011的最簡單方法是什麼?在java中將dd-MMM-yyyy轉換爲dd-MM-yyyy


謝謝大家。這似乎解決了這個問題:

try { 
Calendar cal = Calendar.getInstance(); 
cal.setTime(new SimpleDateFormat("dd-MMM-yyyy").parse("24-Nov-2002")); 
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy"); 
System.out.println(sdf.format(cal.getTime())); 
} catch (ParseException e) { e.printStackTrace(); } 
+0

謝謝大家發現answer.'try { \t \t \t日曆CAL = Calendar.getInstance(); (新的SimpleDateFormat(「dd-MMM-yyyy」)。parse(「2002年11月24日」));以及其他的新的SimpleDateFormat(「dd-MMM-yyyy」)。 \t \t \t SimpleDateFormat sdf = new SimpleDateFormat(「dd-MM-yyyy」); \t \t \t System.out.println(sdf.format(cal.getTime())); ();}}}}}}} catch(ParseException e){ \t} catch(ParseException e){ \t \t \t e.printStackTrace(); \t \t}' – 2011-03-23 05:45:58

回答

6

你看過SimpleDateFormat

+0

是的,我做到了。但是得到一個java.text.ParseException – 2011-03-23 05:37:17

+0

請在這裏發佈您的代碼 – 2011-03-23 05:38:03

+0

感謝您的幫助。 – 2011-03-23 05:50:29

1

這是你的代碼的簡化版本:

DateFormat shortFormat = new SimpleDateFormat("dd-MM-yyyy",Locale.ENGLISH); 
DateFormat mediumFormat = new SimpleDateFormat("dd-MMM-yyyy",Locale.ENGLISH); 
String s = "23-Mar-2011"; 
String shortDate = shortFormat.format(mediumFormat.parse(s)); 
System.out.println(shortDate); 
0

的SimpleDateFormat格式1 =新的SimpleDateFormat( 「DD-MMM-YYYY」);

SimpleDateFormat format2 = new SimpleDateFormat(「dd-MM-yyyy」);

Date date = format1.parse(「2002年11月24日」);

System.out.println(format2.format(date));

6

試試這個

String strDate="23-Mar-2011"; 
SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MMM-yyyy"); 
try { 
    Date varDate=dateFormat.parse(strDate); 
    dateFormat=new SimpleDateFormat("dd-MM-yyyy"); 
    System.out.println("Date :"+dateFormat.format(varDate)); 
}catch (Exception e) { 
    // TODO: handle exception 
    e.printStackTrace(); 
}