Java Events Calendar January 31st
I have the following code: -
Calendar calc = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("MMM-yyyy");
calc.set(Calendar.YEAR, calc.get(Calendar.YEAR) - 1);
calc.set(Calendar.MONTH, Calendar.NOVEMBER);
System.out.println("---NOV? : " + sdf.format(calc.getTime()));
Calendar calc1 = Calendar.getInstance();
calc1.set(Calendar.YEAR, calc1.get(Calendar.YEAR) - 1);
calc1.set(Calendar.MONTH, Calendar.DECEMBER);
System.out.println("-- DEC : " + sdf.format(calc1.getTime()));
Output of the above code: -
> ---NOV? : Dec-2012
> -- DEC : Dec-2012
This only happens on January 31st, can anyone explain why this might be happening?
+3
pranky64
source
to share
2 answers
The calendar is set up for a soft interpretation, so if you tell it to the 31st day of November, well, November only has 30 days, so it will roll over until December 1st.
+7
Affe
source
to share
I suspect that the first case is being rounded from November 31st to "December 1st" since you are not changing the day on your calendar.
+5
Krease
source
to share