How to initialize a calendar object in Java?

In Java, there are several ways to initialize a calendar object:

  1. Get a calendar object in the default time zone using the Calendar.getInstance() method.
  2. Create an instance of the Calendar class.
  3. Get the calendar object for a specific time zone using the Calendar.getInstance(TimeZone) method.
  4. Create a calendar set to the time zone of “Asia/Shanghai.”
  5. Use the Calendar.getInstance(Locale) method to obtain a calendar object for a specific region.
  6. Set the locale to China and get the current calendar instance based on that locale.
  7. Set the year, month, and day using the Calendar.set(year, month, day) method.
  8. 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.

Leave a Reply 0

Your email address will not be published. Required fields are marked *


广告
Closing in 10 seconds
bannerAds