Checking if LocalDateTIme is null in a conditional

We Are Going To Discuss About Checking if LocalDateTIme is null in a conditional. So lets Start this Java Article.

Checking if LocalDateTIme is null in a conditional

  1. Checking if LocalDateTIme is null in a conditional

    Method parse(CharSequence, DateTimeFormatter) of the java.time.LocalDateTime class does not accept null as the character sequence (first parameter), so you have to make an explicit null-check and rewrite the return from your function as
    return if (dateEmailSent != null) LocalDateTime.parse(dateEmailSent, formatter) else null

  2. Checking if LocalDateTIme is null in a conditional

    Method parse(CharSequence, DateTimeFormatter) of the java.time.LocalDateTime class does not accept null as the character sequence (first parameter), so you have to make an explicit null-check and rewrite the return from your function as
    return if (dateEmailSent != null) LocalDateTime.parse(dateEmailSent, formatter) else null

Solution 1

Method parse(CharSequence, DateTimeFormatter) of the java.time.LocalDateTime class does not accept null as the character sequence (first parameter), so you have to make an explicit null-check and rewrite the return from your function as

return if (dateEmailSent != null) LocalDateTime.parse(dateEmailSent, formatter) else null

(you don’t have to have semicolons (;) by the way)

Moreover, the return type of your function as written is non-nullable, so you’ll have to change it to LocalDateTime?.

And, since there is no point in creating a parser/formatter if it is not going to be used in case of dateEmailSent being null, I suggest to rewrite the entire function as follows:

fun timeFormatted(dateEmailSent: String?) = if (dateEmailSent != null) {
    LocalDateTime.parse(dateEmailSent, DateTimeFormatter.ofPattern("yyyy-MM-dd:HH:mm:ss"))
} else null

The last part is up to you, the rest is pretty much mandatory if you want to achieve the functionality you described in your question.

Original Author Of This Content

Solution 2

Assuming that dateSentFormatted is a variable of type LocalDateTime, you can check if it’s null:

if (dateSentFormatted != null ) {
    // dateSentFormatted is not null
}

You don’t need the first !.

Original Author Of This Content

Solution 3

As written, dateSentFormatted can’t be null: you’re setting it to the return from timeFormatted(), which returns a LocalDateTime, which is not nullable. (Unlike LocalDateTime?, which would be.)

And because you haven’t specified its type, the compiler infers LocalDateTime (so even if it were a var, it could still never be null).

So as written, there’s no point in checking for null, and your IDE will warn you that the check’s pointless!

However, if it were nullable, then there are various options for null-checking, depending on what you want to do with it:

  • A simple if (dateSentFormatted != null) test, as per your code (but without the extraneous !). This is the traditional way, and it still sometimes the clearest.

  • Other if () tests. Kotlin’s standard library provides a few ways to make null checks more concise and/or more readable, e.g.:

    if (someString.isNullOrEmpty())
        // …
    
  • The elvis operator ?:, which yields its left-hand side if that’s not null, else its right-hand side. This is useful for supplying a default/fallback value if one’s not specified, e.g.:

    someFunctionCall(dateSentFormatted ?: LocalDateTime.now())
    

    which means roughly the same as:

    someFunctionCall(if (dateSentFormatted != null) dateSentFormatted else LocalDateTime.now())
    
  • The safe-call operator ?., which calls a method/getter only if the LHS is not null, else it gives null directly. This is useful when following an object chain, e.g.:

    val postcode = employee?.manager?.address?.postcode
    

    which means roughly the same as:

    val postcode = if (employee == null)
        null
    else if (employee.manager == null)
        null
    else if (employee.manager.address == null)
        null
    else
        employee.manager.address.postcode
    

    …except that it won’t fail if any of those objects get updated at the same time.

  • The non-null assertion operator !!. If are sure that the value can’t be null at this point, this operator tells the compiler that you know better. (If you’re wrong, you’ll get a runtime exception.) For example:

    someFunctionCall(dateSentFormatted!!)
    

    The potential for runtime errors means that this is rarely a good choice (which is why they made it look scary).

  • Something more complex, depending on what you’re trying to do…

Original Author Of This Content

Conclusion

So This is all About This Tutorial. Hope This Tutorial Helped You. Thank You.

Also Read,

Siddharth

I am an Information Technology Engineer. I have Completed my MCA And I have 4 Year Plus Experience, I am a web developer with knowledge of multiple back-end platforms Like PHP, Node.js, Python and frontend JavaScript frameworks Like Angular, React, and Vue.

Leave a Comment