Java Utility Library

Java Calendar - isSet() Method



The java.util.Calendar.isSet() method is used to determine if the given calendar field has a value set, including cases that the value has been set by internal fields calculations triggered by a get method call.

Syntax

public final boolean isSet(int field)

Parameters

field Specify the calendar field to test.

Return Value

Returns true if the given calendar field has a value set; false otherwise.

Exception

NA.

Example:

In the example below, the java.util.Calendar.isSet() method is used to check if the given calendar field has a value set.

import java.util.*;

public class MyClass {
  public static void main(String[] args) {
    //creating a Calendar object with specified date
    Calendar Cal = new GregorianCalendar(2015, 10, 25);

    //printing the Calendar
    System.out.println("The Calendar is: " + Cal.getTime());

    //printing the year
    System.out.println("The Year is: " + Cal.get(Calendar.YEAR));   

    //checking if the YEAR field has a value set
    System.out.println("The Year is set: " + Cal.isSet(Calendar.YEAR));  

    //clear the year
    Cal.clear(Calendar.YEAR);

    //checking if the YEAR field has a value set
    System.out.println("The Year is set: " + Cal.isSet(Calendar.YEAR));   
  }
}

The output of the above code will be:

The Calendar is: Wed Nov 25 00:00:00 UTC 2015
The Year is: 2015
The Year is set: true
The Year is set: false

❮ Java.util - Calendar