Java Calendar before() Method



Description

The Java Calendar before() method returns whether this Calendar's time is before the time represented by the specified Object (when).

Declaration

Following is the declaration for java.util.Calendar.before() method

public boolean before(Object when)

Parameters

  • when − the Object of time that is about to be compared.

Return Value

true if the time represented by this Calendar is before the time represented by when Object; false otherwise.

Exception

NA

Comparing a Calendar Instance being Before Current Dated Calendar Instance Example

The following example shows the usage of Java Calendar before() method. We're creating two Calendar instances of current date. One of the calendar is modified for past date and then compared using before() method.

package com.tutorialspoint;

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

public class CalendarDemo {
   public static void main(String[] args) {
   
      // create calendar objects.
      Calendar cal = Calendar.getInstance();
      Calendar past = Calendar.getInstance();

      // print the current date
      System.out.println("Current date: " + cal.getTime());

      // change year in past calendar
      past.set(Calendar.YEAR, 2015);
      System.out.println("Year is " + past.get(Calendar.YEAR));

      // check if calendar date is before current date
      Date time = past.getTime();
      
      if (past.before(cal)) {
         System.out.println("Date " + time + " is before current date.");
      }
   }
}

Output

Let us compile and run the above program, this will produce the following result −

Current date: Fri Sep 23 14:47:36 IST 2022
Year is 2015
Date Wed Sep 23 14:47:36 IST 2015 is before current date.

Comparing A Calendar Instance being After Current Dated Calendar Instance Example

The following example shows the usage of Java Calendar before() method. We're creating two Calendar instances of current date. One of the calendar is modified for future date and then compared using before() method.

package com.tutorialspoint;

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

public class CalendarDemo {
   public static void main(String[] args) {
   
      // create calendar objects.
      Calendar cal = Calendar.getInstance();
      Calendar future = Calendar.getInstance();

      // print the current date
      System.out.println("Current date: " + cal.getTime());

      // change year in future calendar
      future.set(Calendar.YEAR, 2025);
      System.out.println("Year is " + future.get(Calendar.YEAR));

      // check if calendar date is before current date
      Date time = future.getTime();
      
      if (future.before(cal)) {
         System.out.println("Date " + time + " is before current date.");
      }else{
	     System.out.println("Date " + time + " is after current date.");
      }
   }
}

Output

Let us compile and run the above program, this will produce the following result −

Current date: Fri Sep 23 14:51:50 IST 2022
Year is 2025
Date Tue Sep 23 14:51:50 IST 2025 is after current date.

Comparing A Calendar Instance being Before Current Dated Calendar Instance Example

The following example shows the usage of Java Calendar before() method. We're creating two Calendar instances of current date. One of the calendar is modified for past date and then compared using before() method on current dated calendar instance.

package com.tutorialspoint;

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

public class CalendarDemo {
   public static void main(String[] args) {
   
      // create calendar objects.
      Calendar cal = Calendar.getInstance();
      Calendar past = Calendar.getInstance();

      // print the current date
      System.out.println("Current date: " + cal.getTime());

      // change year in past calendar
      past.set(Calendar.YEAR, 2015);
      System.out.println("Year is " + past.get(Calendar.YEAR));

      // check if calendar date is before current date
      Date time = past.getTime();
      
      if (cal.before(past)) {
         System.out.println("Date " + time + " is after current date.");
      }else{
	     System.out.println("Date " + time + " is before current date.");
      }
   }
}

Output

Let us compile and run the above program, this will produce the following result −

Current date: Fri Sep 23 14:52:51 IST 2022
Year is 2015
Date Wed Sep 23 14:52:51 IST 2015 is before current date.
java_util_calendar.htm
Advertisements