Saturday, April 4, 2020

Calendar Class in Java with examples

Calendar Class in Java with examples

The java.util.Calendar class implements Serializable, Cloneable, Comparable<Calendar> interfaces.

The java.util.Calendar class is an abstract class and specifies various methods for enabling conversion between a specified moment in the time and a set of different calendar components such as YEAR, MONTH, DAY_OF_MONTH, HOUR, etc. We can manipulate the calendar components, like getting the date of the next week. A specific moment in the time is represented with a millisecond value, which is basically offset from the Epoch (1st January, 1970 00:00:00.000 GMT).

The getInstance() method

The java.util.Calendar class specifies getInstance() class method to get an object of this type. The getInstance() method returns a type Calendar object with calendar fields initialized with the current date and time values.

Calendar thisMoment = Calendar.getInstance();

We can use the Calendar class to generate all the filed values required to implement the date and time formatting in any specified language, even in calendar style (Japanese-Gregorian, Thai Buddhist, etc).

Other versions of the getInstance() method are,

Calendar.getInstance(TimeZone timezone)

Calendar.getInstance(Locale locale)

Calendar.getInstance(TimeZone timezone, Locale locale)

import java.util.Calendar;
import java.util.TimeZone;

public class DateDemo {

 public static void main(String[] args) {
                Calendar thisMoment=Calendar.getInstance();
                System.out.println("The time right now is: "+thisMoment.getTime());
                Calendar byTimeZone=Calendar.getInstance(TimeZone.getDefault());
                System.out.println("The time right now is: "+byTimeZone.getTime());
     }
}
Output:

The time right now is: Sat Apr 04 16:54:06 IST 2020
The time right now is: Sat Apr 04 16:54:06 IST 2020



Setting the Fields in Calender

We can use set methods to set various fields in a calendar object. For example,

public void set(int field, int val)
public final void set(int year, int month, int date)
public final void set(int year, int month, int date, int hour_Of_Day, int min)
public final void set(int year, int month, int date, int hour_Of_Day, int min, int sec)
public final void setTime(Date date) etc,

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

public class DateDemo {

     public static void main(String[] args) {
                Calendar thisMoment=Calendar.getInstance();
      
                /*
                 * SET the Date as 12 June 2021
                 * by thisMoment.set(year, month, date) method
                 */
           thisMoment.set(2021, 4, 12);
 System.out.println("The time right mow is: "+thisMoment.getTime());
           //Setting the Time by setTime method
     thisMoment.setTime(new Date(1612323215621L));
     System.out.println("The updated time: "+thisMoment.getTime());
     }
}
Output:
The time right mow is: Wed May 12 17:37:43 IST 2021
The updated time: Wed Feb 03 09:03:35 IST 2021


Getting the fields from Calender

We can use get method to get the various fields from the Calendar object.

public int get(int field)

This method returns the value of the given calendar field. In lenient mode, all calendar fields are normalized. In non-lenient mode, all calendar fields are validated and this method throws an exception if any calendar fields have out-of-range values.

import java.util.Calendar;

public class DateDemo {

     public static void main(String[] args) {
            Calendar thisMoment=Calendar.getInstance();
            System.out.println(thisMoment.get(Calendar.DAY_OF_MONTH));
            System.out.println(thisMoment.get(Calendar.DAY_OF_WEEK));
    System.out.println(thisMoment.get(Calendar.DAY_OF_WEEK_IN_MONTH));
            System.out.println(thisMoment.get(Calendar.DAY_OF_YEAR));
            System.out.println(thisMoment.get(Calendar.DATE));
            System.out.println(thisMoment.get(Calendar.HOUR));
            System.out.println(thisMoment.get(Calendar.MINUTE));
            System.out.println(thisMoment.get(Calendar.SECOND));
            System.out.println(thisMoment.get(Calendar.MILLISECOND));
            System.out.println(thisMoment.get(Calendar.HOUR_OF_DAY));
            System.out.println(thisMoment.get(Calendar.ZONE_OFFSET));
     }
}
Output:
4
7
1
95
4
6
1
44
982
18
19800000

Other important get methods are

import java.util.Calendar;

public class DateDemo {

     public static void main(String[] args) {
                Calendar thisMoment=Calendar.getInstance();
                //Other useful get methods
            System.out.println(thisMoment.getCalendarType());
            System.out.println(thisMoment.getMinimum(Calendar.DAY_OF_MONTH));
            System.out.println(thisMoment.getMaximum(Calendar.DAY_OF_WEEK));
            System.out.println(thisMoment.getFirstDayOfWeek());
            System.out.println(thisMoment.getWeekYear());
            System.out.println(thisMoment.getTimeInMillis());

     }
}
Output:
Gregory
1
7
1
2020
1586004305229


The add() method

We can add or subtract(Modify) the Calendar fields using the add method. For example,

import java.util.Calendar;

public class DateDemo {

      public static void main(String[] args) {
       Calendar thisMoment=Calendar.getInstance();
       //using the add method
        ystem.out.println("Today's date:"+thisMoment.getTime());
        thisMoment.add(Calendar.DATE, -10);
     system.out.println("Date 10 days earlier:"+thisMoment.getTime());
      thisMoment.add(Calendar.DATE, 20);
     System.out.println("Date 10 days later:"+thisMoment.getTime());
                  // We can apply these operations to
                  //any fields like MONTH, HOUR, DATE, SECOND, YEAR, etc.
      }
}
Output:
Today's date:Sat Apr 04 18:24:24 IST 2020
Date 10 days earlier:Wed Mar 25 18:24:24 IST 2020
Date 10 days later:Tue Apr 14 18:24:24 IST 2020



We can apply these operations to any of the Calendar fields like MONTH, HOUR, DATE, SECOND, YEAR, etc to add or subtract the fields.