January 31 of java calendar

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()));

The output of the above code is: –

> ---NOV? : Dec-2012
> -- DEC : Dec-2012

This happened only on January 31. Can anyone explain why this happened?

Solution

The calendar is set to loose interpretation, so if you tell it November 31, November has only 30 days, so it will turn to December 1

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>