2013-02-19 23 views
2

爲什麼此代碼失敗?目的是消除時間部分。Calendar.getTime()失敗,返回java.lang.IllegalArgumentException:亞洲/新加坡時區爲MINUTE

String dateStr = "1982-01-01"; 
String timeZoneID = "Asia/Singapore"; 

DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); 
Date date = dateFormat.parse(dateStr);  

Calendar calendar = Calendar.getInstance(); 
calendar.setTimeZone(TimeZone.getTimeZone(timeZoneID)); 
calendar.setLenient(false); 
calendar.setTime(date); 
calendar.set(Calendar.HOUR_OF_DAY, 0); 
calendar.set(Calendar.MINUTE, 0); 
calendar.set(Calendar.SECOND, 0); 
calendar.set(Calendar.MILLISECOND, 0); 

System.out.println(calendar.getTime()); 

錯誤消息:

Exception in thread "main" java.lang.IllegalArgumentException: MINUTE 
    at java.util.GregorianCalendar.computeTime(GregorianCalendar.java:2482) 
    at java.util.Calendar.updateTime(Calendar.java:2265) 
    at java.util.Calendar.getTimeInMillis(Calendar.java:1049) 
    at java.util.Calendar.getTime(Calendar.java:1022) 
    at Prog.main(Prog.java:31) 

它工作正常以下輸入:

  • dateStr = 「1982年1月1日」,timeZoneID = 「歐洲/柏林」
  • dateStr =「1981-01-01」,timeZoneID =「Asia/Singapore」
  • dateStr =「1982-01-01」,timeZoneID =「Asia/Seoul」
+0

因爲新加坡標準時間「開始」在1/1/82? http://www.math.nus.edu.sg/aslaksen/teaching/timezone.html – Nivas 2013-02-19 22:15:20

回答

6

你的代碼開始的日期1982年1月1日,並將兩HOUR_OF_DAY和MINUTE爲0

但沒有12:00:00 AM 1982年1月1日,在新加坡舉行。 1981年12月31日晚上11點59分59秒之後,新加坡向上跳了半個小時,直到上午12點半。它之前一直在UTC + 7:30,但移至UTC + 8的全時區。

來源:Singapore Standard Timetimeanddate.com。請參閱Why is Singapore in the "wrong" time zone?瞭解新加坡的簡要時間。

相關問題