The java.util package library contains the GregorianCalendar class that implements the calendar system used by most of the world. It extends the Calendar class and provides methods for manipulating dates, performing calendar-related computations, and checking leap years.
Some code examples using the GregorianCalendar class are:
1. Creating a new instance:
GregorianCalendar calendar = new GregorianCalendar();
2. Setting a specific date:
GregorianCalendar calendar = new GregorianCalendar(2021, 11, 25); //Month is zero-indexed, so 11 represents December.
3. Getting the current date:
int year = calendar.get(Calendar.YEAR); int month = calendar.get(Calendar.MONTH) + 1; int day = calendar.get(Calendar.DAY_OF_MONTH);
4. Adding or subtracting days, months, or years:
calendar.add(Calendar.MONTH, 1); //Adds one month to the current date.
5. Checking if a year is a leap year:
boolean isLeapYear = calendar.isLeapYear(year);
These examples illustrate the flexibility of the GregorianCalendar class and how it can be used to perform various calendar-related tasks.
Java GregorianCalendar - 30 examples found. These are the top rated real world Java examples of java.util.GregorianCalendar extracted from open source projects. You can rate examples to help us improve the quality of examples.