Home > Mobile >  How to check for empty values in Calendar object?
How to check for empty values in Calendar object?

Time:09-07

I am using calendar API to compare dates as so:

Date date = DateHelper.getDate(stringDate) This returns a date object
Calendar atPtmtCalendar = Calendar.getInstance(); 
if(date != null){
    atPtmtCalendar.setTime(date)
}

The problem is that I am currently checking the variable date for null values rather than atPmtCalendar as so:

if(date != null){
     Boolean isBefore = atPtmtCalendar.before(otherCalendarDate)
}

Is there any way to check for null values by using atPtmtCalendar?

CodePudding user response:

tl;dr

No, you cannot use a possibly-null reference variable itself to see if it is null. You must check from outside the variable.

Objects                     // Handle utility class.
.requireNonNull( myDate )   // Throws exception if reference variable holds no reference, is null.
.toInstant()                // Convert from terrible legacy class `java.util.Date` to modern class `java.time.Instant`. 
.isBefore(                  // Compare one `Instant` object to another.
    Instant.now()           // Capture the current moment as seen with an offset of zero hours-minutes-seconds from UTC.
)                           // Returns true or false.

Avoid legacy date-time classes

The Date & Calendar classes are terribly flawed. They were years ago supplanted by the modern java.time classes defined in JSR 310.

java.time.Instant

When handed a java.util.Date object, immediately convert to its replacement, java.time.Instant. Use the new conversion methods added to the old classes.

Instant instant = myJavaUtilDate.toInstant() ;

For comparing, call any of the Instant methods: equals, isBefore, isAfter.

Null-check

As for checking for nulls… If your object reference such as Calendar atPtmtCalendar is null then you cannot call any methods with which to check for nulls. So your question “Is there any way to check for null values by using atPtmtCalendar?” makes no sense. So your null-check must be separate from your use of the possibly null variable.

I would prefer directly replacing your null check with a call to Objects.nonNull for easier reading.

if( null != myVar ) { … }              // Not my favorite.
if( Objects.nonNull( myVar ) ) { … }   // Preferred by me.

If the variable should never be null, then throw an exception if it is indeed null. To do so, call Objects.requireNonNull. Optionally, you can pass text for the error message within the exception.

if( Objects.requireNonNull( myVar ) ) { … } 

If null is a legitimate value in your particular scenario, then your called method should return an Optional.

Optional< Date > date = whatever.getDate() ;

Your code can then use any of the several handy methods on Optional to handle the case of a missing payload.

Example code

Putting all this together:

boolean isPastDate = 
    Objects
    .requireNonNull( myDate )
    .toInstant()
    .isBefore( 
        Instant.now() 
    ) 
;
  • Related