.isSet()
The .isSet()
method is used to evaluate if a current field has a value or not. The return type is a Bool
, and it returns true
if the given field has a value set and false
if it isn’t. It returns true
when the given parameter has been set by internal field interpretations. isSet()
belongs to the Calendar
class and is a non-static method, it is accessible with the class object.
Syntax
result = myCalendar.isSet(calendar_field)
The method takes one parameter, calendar_field
, that refers to the specific field to be operated upon.
The method either returns true
if the value of the field is set, else it returns false
.
Note: The
Calendar
class has been replaced by theLocalDate
,LocalTime
, andLocalDateTime
classes in the Java 8 and later Date-Time API. These classes provide a more modern and user-friendly API for working with dates and times in Java.
Example
The following example shows the usage of java.util.calendar.isSet()
method.
import java.util.*;public class CalendarDemo {public static void main(String[] args) {// Create a calendarCalendar cal = Calendar.getInstance();// Display the current calendarSystem.out.println("Current Day is " + cal.get(Calendar.DAY_OF_MONTH));// Determine if the given calendar field has a value setboolean b = cal.isSet(Calendar.DAY_OF_MONTH);System.out.println("Day of month is set: " + b);// Clear day of monthcal.clear(Calendar.DAY_OF_MONTH);// Determine if the given calendar field has a value setb = cal.isSet(Calendar.DAY_OF_MONTH);System.out.println("Day of month is set: " + b);}}
This code will return an output similar to the following:
Current Day is 6Day of month is set: trueDay of month is set: false
Looking to contribute?
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.