2009-06-22 31 views
1

我試圖解析一個MySql格式的數據,我碰到了SimpleDateFormat。我能得到正確的日和月,但我得到了一年一個奇怪的結果:爲什麼我不能在Java中使用SimpleDateFormat的一年?

date = 2009-06-22; 
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); 
Date d = sdf.parse(date); 
System.println(date); 
System.println(d.getDate()); 
System.println(d.getMonth()); 
System.println(d.getYear()); 

輸出:

2009-06-22 
22   OK 
5   Hum... Ok, months go from 0 to 11 
109   o_O WTF ? 

我試圖改變格式YYYY-MM-dd(得到一個錯誤)和yy-MM-dd(什麼也沒做)。我在Android上編程,不知道它是否重要。

現在,我繞過使用拆分,但它很髒,並阻止我使用國際化的功能。

回答

12

年相對於1900年。這是Date類的「特徵」。嘗試使用Calender

+0

好,非常感謝,但我怎麼能解析使用壓延機的日期? – 2009-06-22 16:32:42

5

由於阿龍,正確的版本:

Calendar c = Calendar.getInstance(); 
c.setTime(sdf.parse(date)); 
System.println(c.get(Calendar.YEAR)); 
相關問題