2012-05-31 35 views
2

我作爲字符串日期的列表,即:Java的日期解析:歐洲中部時間下午2:00和2:00 28/10/2012 CEST 28/10/2012區分

28/10/2012 00:00 
28/10/2012 01:00 
28/10/2012 02:00 
28/10/2012 02:00 
28/10/2012 03:00 

(同一時刻兩次是因爲DST)

而且我需要確保每個日期之間只有一個小時。

問題在於,第一個28/10/2012 02:00是CEST,而第二個是CET。區分它們是很容易的部分,因爲第一個是CEST,而第二個是CET。難的部分是我怎樣才能指定到SimpleDateFormat(或另一個日期解析類)該字符串表示一個CEST/CET時間,所以我可以得到正確的Date對象?

在此先感謝!

回答

4

考慮下面的例子 我認爲它有你的答案

convert String to Date with SimpleDateFormat considering CET CEST

public class DatesAndTimesStackOverflow { 

final static SimpleDateFormat sdf; 
final static TimeZone tz; 
static { 
    tz = TimeZone.getTimeZone("Europe/Paris"); 
    sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss zzz"); 
    sdf.setTimeZone(tz); 
} 

public static void main(String[] args) { 

    // october clock change should be the following: 
    outputDateInfo("2012-10-28 02:00:00 CEST"); 
    outputDateInfo("2012-10-28 02:30:00 CEST"); 
    outputDateInfo("2012-10-28 02:00:00 CET"); 
    outputDateInfo("2012-10-28 02:30:00 CET"); 
    outputDateInfo("2012-10-28 03:00:00 CET"); 
    outputDateInfo("2012-10-28 03:30:00 CET"); 
    outputDateInfo("2012-10-28 04:00:00 CET"); 
} 


private static void outputDateInfo(String theDate) { 
    try { 
     output("------------------------------------------------------------------------------"); 
     Date d = sdf.parse(theDate); 
     Calendar c = GregorianCalendar.getInstance(tz); 
     c.setTimeInMillis(d.getTime()); 
     TimeZone tzCal = c.getTimeZone(); 

     output("String:      " + theDate); 
     output(""); 
     output("Date:       " + d);     // toString uses current system TimeZone 
     output("Date Millis:     " + d.getTime()); 
     output("Cal Millis:     " + c.getTimeInMillis()); 
     output("Cal To Date Millis:   " + c.getTime().getTime()); 
     output("Cal TimeZone Name:   " + tzCal.getDisplayName()); 
     output("Cal TimeZone ID:    " + tzCal.getID()); 
     output("Cal TimeZone DST Name:  " + tzCal.getDisplayName(true, TimeZone.SHORT)); 
     output("Cal TimeZone Standard Name: " + tzCal.getDisplayName(false, TimeZone.SHORT)); 
     output("In DayLight:     " + tzCal.inDaylightTime(d)); 

     output(""); 
     output("Day Of Month:     " + c.get(Calendar.DAY_OF_MONTH)); 
     output("Month Of Year:    " + c.get(Calendar.MONTH)); 
     output("Year:       " + c.get(Calendar.YEAR)); 

     output("Hour Of Day:     " + c.get(Calendar.HOUR_OF_DAY)); 
     output("Minute:      " + c.get(Calendar.MINUTE)); 
     output("Second:      " + c.get(Calendar.SECOND)); 

     // check to see if this converts back to correct string 
     String reformat = sdf.format(c.getTime()); 
     if(reformat.equals(theDate)) { 
      output("ReConvert:     " + reformat + " OK"); 
     } else { 
      output("ReConvert:     " + reformat + " <-------- Error. The converted date is different"); 
     } 

    } catch (ParseException ex) { 
     output("Cannot parse this date"); 
    } 
} 

private static void output(String message) { 
    System.out.println(message); 
} 
}