Why is this code failing? The purpose was to remove the time part.
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());
The error message:
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)
It works fine for the following input:
- dateStr = "1982-01-01", timeZoneID = "Europe/Berlin"
- dateStr = "1981-01-01", timeZoneID = "Asia/Singapore"
- dateStr = "1982-01-01", timeZoneID = "Asia/Seoul"