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