Q:

When trying to get last date of next month getting last date of next month of next year in java

Trying to run this code it was working perfectly fine till OCT but in NOV it is like 
firstdate 2019-12-01 & lastdate  2020-12-31
public class Test1 {
    public static void main(String[] args) {
        Calendar calendar = Calendar.getInstance();         
        calendar.add(Calendar.MONTH, 1);
        String date;    
        calendar.set(Calendar.DATE,calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
        Date nextMonthFirstDay = calendar.getTime();
        date=new SimpleDateFormat("YYYY-MM-dd").format(nextMonthFirstDay).toLowerCase();
        System.out.println("firstdate "+ date);

        calendar.set(Calendar.DAY_OF_MONTH,calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
        Date nextMonthLastDay = calendar.getTime();
        date=new SimpleDateFormat("YYYY-MM-dd").format(nextMonthLastDay).toLowerCase();
        System.out.println("lastdate  "+date);

    }
}

I don't know why it is showing like this..
Is it a fault or bug in java ?

A:

Change your date format to yyyy-MM-dd (notice lowercase for year)
They both represent a year but yyyy represents the calendar year while YYYY represents the year of the week.
So something like...
date=new SimpleDateFormat("yyyy-MM-dd").format(nextMonthLastDay).toLowerCase();

Hope it helps!

