Why Java Calendary don't execute some lines? : Java don't execute some lines of my code when I use Calendary library.

I'm trying to get the date of monday before 1 of actual month.

//Today is Tuesday, 2 January of 2019 (29/01/2019)

    1. Calendar cp1 = GregorianCalendar.getInstance();
    2. cp1.set(Calendar.DAY_OF_MONTH, 1); //THIS LINE DON'T WORKS
    3. cp1.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
    4. SimpleDateFormat sdf=new SimpleDateFormat ("dd/MM/yyyy");
    5. System.out.println(sdf.format (cp1.getTime()));
 
//return 28/01/2019 instead of 31/12/2018.


//IF I ADD System.out.println (cp1) after line 2 java don't jump line 2 and works well.

//Today is Tuesday, 2 January of 2019 (29/01/2019)

    1. Calendar cp1 = GregorianCalendar.getInstance();
    2. cp1.set(Calendar.DAY_OF_MONTH, 1); //THIS LINE WORKS NOW
    3. System.out.println (cp1);
    4. cp1.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
    5. SimpleDateFormat sdf=new SimpleDateFormat ("dd/MM/yyyy");
    6. System.out.println(sdf.format (cp1.getTime())); 

//return 31/12/2018 that is the correct date.

//Why java didn't execute 2nd line in my first code? Is a java bug?

Expected result "31/12/2018".
Actual result "28/01/2019".

