I am facing a problem I really don't understand. I am trying to use the set method on a Calendar but it modifies the values only if I print it, here is the code:
Calendar cal = Calendar.getInstance();
cal.set(2020,2,31);
// System.out.println("Necessary print: " + cal.getTime());
cal.set(Calendar.DAY_OF_WEEK, cal.getFirstDayOfWeek());
// reset time
cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
System.out.println("Date set: " + cal.getTime());
System.out.println("Day of month: " + cal.get(Calendar.DAY_OF_MONTH));
This previous code gives me the output:
Date set: Mon Mar 02 00:00:00 CET 2020 Day of month: 2
And if I uncomment the "necessary print", then I get:
Necessary print: Tue Mar 31 23:07:17 CEST 2020 Date set: Mon Mar 30 00:00:00 CEST 2020 Day of month: 30
I'm really wondering why a print can affect this method, thanks in advance for help!