Created
June 20, 2022 15:53
-
-
Save ktprezes/b80b0e843ca87daf343aa5299aac1b22 to your computer and use it in GitHub Desktop.
Java GregorianCalendar strange behaviour - the difference between these 2 files is the presence of 'printf' with gregCal.get(...) functions only, and the results are different!!!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Calendar; | |
import java.util.GregorianCalendar; | |
class GregorianCalendarPlayground1 { | |
public static void main(String[ ] args) { | |
Calendar gregCal = new GregorianCalendar(); | |
// wrong date, but the class in default 'lenient' mode should maintain this | |
gregCal.set(0, Calendar.DECEMBER, 31); | |
// according to the 'GregorianCalendar' class description the 'set' method in the default 'lenient' mode | |
// only stores the passed values, without validation, and the next 'get' methods make validation, normalization, etc... | |
// version #1 of the snippet - with the following 'printf'/'get(...)' methods: | |
System.out.printf("%s, %s, %s%n", | |
gregCal.get(Calendar.DAY_OF_WEEK), // prints '6' - which means 'FRIDAY' here | |
gregCal.get(Calendar.DAY_OF_YEAR), // prints '366' - treats the year '0' as the 'leap' year | |
gregCal.get(Calendar.YEAR) // prints '1' !?!?!? | |
); | |
gregCal.set(33, Calendar.APRIL, 3); // this is the first Good Friday in history... | |
System.out.printf("%s, %s, %s%n", | |
gregCal.get(Calendar.DAY_OF_WEEK), // prints '2' which means 'MONDAY' here - what is a total mess.. | |
gregCal.get(Calendar.DAY_OF_YEAR), // prints '94', what is a mess, either (shoud be 93) | |
gregCal.get(Calendar.YEAR) // prints '33' - and this is ok | |
); | |
gregCal.set(2022, Calendar.JUNE, 20); | |
System.out.printf("%s, %s, %s%n", | |
gregCal.get(Calendar.DAY_OF_WEEK), // prints '1' which means 'SUNDAY', what is WRONG - it is MONDAY in fact... | |
gregCal.get(Calendar.DAY_OF_YEAR), // prints '171' what is WRONG, too... | |
gregCal.get(Calendar.YEAR) // prints '2022' - and this is ok... | |
); | |
} // psv main() | |
} // class GregorianCalendarPlayground1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Calendar; | |
import java.util.GregorianCalendar; | |
class GregorianCalendarPlayground1 { | |
public static void main(String[ ] args) { | |
Calendar gregCal = new GregorianCalendar(); | |
// wrong date, but the class in default 'lenient' mode should maintain this | |
gregCal.set(0, Calendar.DECEMBER, 31); | |
// let's comment-out the 'printf' with three 'gregCal.get' methods, and observe the results: | |
// System.out.printf("%s, %s, %s%n", | |
// gregCal.get(Calendar.DAY_OF_WEEK), | |
// gregCal.get(Calendar.DAY_OF_YEAR), | |
// gregCal.get(Calendar.YEAR) | |
// ); | |
gregCal.set(33, Calendar.APRIL, 3); | |
System.out.printf("%s, %s, %s%n", | |
gregCal.get(Calendar.DAY_OF_WEEK), // prints '6' - which means FRIDAY here - and this is OK | |
gregCal.get(Calendar.DAY_OF_YEAR), // prints '93' - and this is OK | |
gregCal.get(Calendar.YEAR) // prints '33' - and this is OK, too... | |
); | |
gregCal.set(2022, Calendar.JUNE, 20); | |
System.out.printf("%s, %s, %s%n", | |
gregCal.get(Calendar.DAY_OF_WEEK), // prints '2' - which means MONDAY here - and this is OK | |
gregCal.get(Calendar.DAY_OF_YEAR), // prints '171' - and this is OK, too... | |
gregCal.get(Calendar.YEAR) // prints '2022' - ok | |
); | |
// so... the presence of a simple 'printf' command with GregorianCalendar.get(...) methods changes the values returned... | |
// as for me, it disqualifies this 'GregorianCalendar' class | |
} // psv main() | |
} // class GregorianCalendarPlayground2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
java.util.Calendar
,java.util.GregorianCalendar
etc., are not marked as@Deprecated
yet, but have been outdated since version 1.8 and the introduction of the newerjava.time
package