toDateOrNull

@JvmOverloads
fun String.toDateOrNull(pattern: String = "EEE MMM dd HH:mm:ss zzz yyyy"): Date?

Produce a date from the given strings value and pattern if possible.

Example:

"8/7/2022".toDateOrNull("MM/dd/yyyy") // Sun Aug 07 00:00:00 IRDT 2022
"Sun Aug 07 16:37:42 IRDT 2022".toDateOrNull() // Sun Aug 07 16:37:42 IRDT 2022
"7/2022".toDateOrNull("MM/dd/yyyy") // null

Since

1.1.0

Return

A nullable Date parsed from the string.

See also