How to initialize a calendar object in Java?
In Java, there are several ways to initialize a calendar object:
- Get a calendar object in the default time zone using the Calendar.getInstance() method.
- Create an instance of the Calendar class.
- Get the calendar object for a specific time zone using the Calendar.getInstance(TimeZone) method.
- Create a calendar set to the time zone of “Asia/Shanghai.”
- Use the Calendar.getInstance(Locale) method to obtain a calendar object for a specific region.
- Set the locale to China and get the current calendar instance based on that locale.
- Set the year, month, and day using the Calendar.set(year, month, day) method.
- int year = 2022;
int month = Calendar.JANUARY; // Months are zero-based, with 0 representing January
int day = 1;
Calendar calendar = Calendar.getInstance();
calendar.set(year, month, day);
Please note that the month range of the Calendar class is from 0 to 11, where 0 represents January, 1 represents February, and so on.