Java Calendar Class

Introduction

The Calendar class in Java, part of the java.util package, provides methods for manipulating dates and times in a calendar system.

Table of Contents

  1. What is the Calendar Class?
  2. Common Methods
  3. Examples of Using the Calendar Class
  4. Conclusion

1. What is the Calendar Class?

The Calendar class is used to convert between a specific instant in time and a set of calendar fields such as year, month, day, and time. It is useful for performing date arithmetic and localization.

2. Common Methods

  • getInstance(): Returns a Calendar object using the default time zone and locale.
  • get(int field): Returns the value of the specified calendar field.
  • set(int field, int value): Sets the specified calendar field to the given value.
  • add(int field, int amount): Adds or subtracts the specified amount of time to the given calendar field.
  • roll(int field, boolean up): Rolls up or down the specified field without changing larger fields.
  • getTime(): Returns a Date object representing the calendar's time value.
  • setTime(Date date): Sets the calendar's time with the given Date object.
  • before(Object when): Returns true if the time represented by this Calendar is before the specified time.
  • after(Object when): Returns true if the time represented by this Calendar is after the specified time.
  • clear(): Resets all fields to zero.

3. Examples of Using the Calendar Class

Example 1: Getting Current Date and Time

import java.util.Calendar;

public class CurrentDateTimeExample {
    public static void main(String[] args) {
        Calendar calendar = Calendar.getInstance();
        System.out.println("Current Date and Time: " + calendar.getTime());
    }
}

Output:

Current Date and Time: Sun Jun 30 18:09:16 IST 2024

Example 2: Setting a Specific Date

import java.util.Calendar;

public class SetDateExample {
    public static void main(String[] args) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.YEAR, 2023);
        calendar.set(Calendar.MONTH, Calendar.JANUARY);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        System.out.println("Set Date: " + calendar.getTime());
    }
}

Output:

Set Date: Sun Jan 01 18:09:16 IST 2023

Example 3: Adding Days to a Date

import java.util.Calendar;

public class AddDaysExample {
    public static void main(String[] args) {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DAY_OF_MONTH, 10);
        System.out.println("Date after adding 10 days: " + calendar.getTime());
    }
}

Output:

Date after adding 10 days: Wed Jul 10 18:09:16 IST 2024

Example 4: Rolling a Month

import java.util.Calendar;

public class RollMonthExample {
    public static void main(String[] args) {
        Calendar calendar = Calendar.getInstance();
        calendar.roll(Calendar.MONTH, true);
        System.out.println("Month after rolling up: " + calendar.getTime());
    }
}

Output:

Month after rolling up: Tue Jul 30 18:09:16 IST 2024

Example 5: Getting Specific Calendar Fields

import java.util.Calendar;

public class GetFieldsExample {
    public static void main(String[] args) {
        Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH) + 1; // Months are 0-based
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        System.out.println("Year: " + year + ", Month: " + month + ", Day: " + day);
    }
}

Output:

Year: 2024, Month: 6, Day: 30

Example 6: Checking If the Date is Before Another

import java.util.Calendar;

public class BeforeExample {
    public static void main(String[] args) {
        Calendar calendar1 = Calendar.getInstance();
        calendar1.set(2023, Calendar.JANUARY, 1);
        Calendar calendar2 = Calendar.getInstance();
        calendar2.set(2024, Calendar.JANUARY, 1);
        boolean isBefore = calendar1.before(calendar2);
        System.out.println("Calendar1 is before Calendar2: " + isBefore);
    }
}

Output:

Calendar1 is before Calendar2: true

Example 7: Checking If Date is After Another

import java.util.Calendar;

public class AfterExample {
    public static void main(String[] args) {
        Calendar calendar1 = Calendar.getInstance();
        calendar1.set(2024, Calendar.JANUARY, 1);
        Calendar calendar2 = Calendar.getInstance();
        calendar2.set(2023, Calendar.JANUARY, 1);
        boolean isAfter = calendar1.after(calendar2);
        System.out.println("Calendar1 is after Calendar2: " + isAfter);
    }
}

Output:

Calendar1 is after Calendar2: true

Example 8: Clearing Calendar Fields

import java.util.Calendar;

public class ClearExample {
    public static void main(String[] args) {
        Calendar calendar = Calendar.getInstance();
        calendar.clear();
        System.out.println("Calendar after clearing: " + calendar.getTime());
    }
}

Output:

Calendar after clearing: Thu Jan 01 00:00:00 IST 1970

Example 9: Setting Time Using Date

import java.util.Calendar;
import java.util.Date;

public class SetTimeExample {
    public static void main(String[] args) {
        Calendar calendar = Calendar.getInstance();
        Date date = new Date();
        calendar.setTime(date);
        System.out.println("Calendar set with Date: " + calendar.getTime());
    }
}

Output:

Calendar set with Date: Sun Jun 30 18:09:17 IST 2024

Example 10: Getting the Week of Year

import java.util.Calendar;

public class WeekOfYearExample {
    public static void main(String[] args) {
        Calendar calendar = Calendar.getInstance();
        int weekOfYear = calendar.get(Calendar.WEEK_OF_YEAR);
        System.out.println("Week of the Year: " + weekOfYear);
    }
}

Output:

Week of the Year: 27

4. Conclusion

The Calendar class in Java provides powerful methods for date and time manipulation, allowing developers to perform complex date arithmetic, manage time zones, and work with different locales. It is a versatile tool for handling date and time in Java applications.

Comments