检查条件中 LocalDateTIme 是否为 null
Checking if LocalDateTIme is null in a conditional
这里我有一个将字符串格式化为 LocalDateTime 的函数,returns 它。
val dateSentFormatted = timeFormatted(record.data.dateTime);
private fun timeFormatted(dateEmailSent: String?): LocalDateTime {
val formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd:HH:mm:ss");
return LocalDateTime.parse(dateEmailSent, formatter);
}
我的问题是我想要一个 if 语句 运行 在我文件的其他地方检查它是否为空,如:
if (!dateSentFormatted != null ) {
}
但它不喜欢那样,我还能如何检查 if 语句中 LocalDateTime 类型的变量是否为空?
假设dateSentFormatted
是一个LocalDateTime
类型的变量,你可以检查它是否为空:
if (dateSentFormatted != null ) {
// dateSentFormatted is not null
}
您不需要第一个 !
。
如所写,dateSentFormatted
不能为空:您将其设置为 timeFormatted()
中的 return,returns a LocalDateTime
,不可为空。 (与 LocalDateTime?
不同。)
并且因为您没有指定它的类型,编译器会推断出 LocalDateTime
(所以即使它是 var
,它仍然永远不会为 null)。
正如所写,检查 null 没有意义,您的 IDE 会警告您检查毫无意义!
但是,如果它是可为空的,则有多种选项可用于空值检查,具体取决于您要对其执行的操作:
一个简单的 if (dateSentFormatted != null)
测试,根据您的代码(但没有无关的 !
)。这是传统的方式,有时仍然是最清晰的。
其他 if ()
测试。 Kotlin 的标准库提供了一些方法使 null 检查更简洁 and/or 更具可读性,例如:
if (someString.isNullOrEmpty())
// …
elvis 运算符 ?:
,如果它不为空,则生成其左侧,否则生成其右侧。如果未指定,这对于提供 default/fallback 值很有用,例如:
someFunctionCall(dateSentFormatted ?: LocalDateTime.now())
这意味着大致相同:
someFunctionCall(if (dateSentFormatted != null) dateSentFormatted else LocalDateTime.now())
安全调用运算符?.
,仅当LHS不为null时才调用method/getter,否则直接返回null。这在遵循对象链时很有用,例如:
val postcode = employee?.manager?.address?.postcode
这意味着大致相同:
val postcode = if (employee == null)
null
else if (employee.manager == null)
null
else if (employee.manager.address == null)
null
else
employee.manager.address.postcode
…除了如果这些对象中的任何一个同时更新它不会失败。
非空断言运算符!!
。如果此时确定该值不能为 null,则此运算符会告诉编译器您知道得更多。 (如果你错了,你会得到一个运行时异常。)例如:
someFunctionCall(dateSentFormatted!!)
运行时错误的可能性意味着这很少是一个好的选择(这就是为什么他们让它看起来很可怕)。
更复杂的东西,取决于你想做什么……
方法parse(CharSequence, DateTimeFormatter)
的java.time.LocalDateTime
class不接受null
作为字符序列(第一个参数),所以你必须明确null
-检查并将函数中的 return 重写为
return if (dateEmailSent != null) LocalDateTime.parse(dateEmailSent, formatter) else null
(顺便说一句,你不必有分号(;
))
此外,您编写的函数的 return 类型不可为空,因此您必须将其更改为 LocalDateTime?
。
而且,如果在 dateEmailSent
为 null
的情况下不使用它,那么创建 parser/formatter 是没有意义的,我建议重写整个函数如下:
fun timeFormatted(dateEmailSent: String?) = if (dateEmailSent != null) {
LocalDateTime.parse(dateEmailSent, DateTimeFormatter.ofPattern("yyyy-MM-dd:HH:mm:ss"))
} else null
最后一部分由您决定,如果您想实现问题中描述的功能,其余部分几乎是强制性的。
这里我有一个将字符串格式化为 LocalDateTime 的函数,returns 它。
val dateSentFormatted = timeFormatted(record.data.dateTime);
private fun timeFormatted(dateEmailSent: String?): LocalDateTime {
val formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd:HH:mm:ss");
return LocalDateTime.parse(dateEmailSent, formatter);
}
我的问题是我想要一个 if 语句 运行 在我文件的其他地方检查它是否为空,如:
if (!dateSentFormatted != null ) {
}
但它不喜欢那样,我还能如何检查 if 语句中 LocalDateTime 类型的变量是否为空?
假设dateSentFormatted
是一个LocalDateTime
类型的变量,你可以检查它是否为空:
if (dateSentFormatted != null ) {
// dateSentFormatted is not null
}
您不需要第一个 !
。
如所写,dateSentFormatted
不能为空:您将其设置为 timeFormatted()
中的 return,returns a LocalDateTime
,不可为空。 (与 LocalDateTime?
不同。)
并且因为您没有指定它的类型,编译器会推断出 LocalDateTime
(所以即使它是 var
,它仍然永远不会为 null)。
正如所写,检查 null 没有意义,您的 IDE 会警告您检查毫无意义!
但是,如果它是可为空的,则有多种选项可用于空值检查,具体取决于您要对其执行的操作:
一个简单的
if (dateSentFormatted != null)
测试,根据您的代码(但没有无关的!
)。这是传统的方式,有时仍然是最清晰的。其他
if ()
测试。 Kotlin 的标准库提供了一些方法使 null 检查更简洁 and/or 更具可读性,例如:if (someString.isNullOrEmpty()) // …
elvis 运算符
?:
,如果它不为空,则生成其左侧,否则生成其右侧。如果未指定,这对于提供 default/fallback 值很有用,例如:someFunctionCall(dateSentFormatted ?: LocalDateTime.now())
这意味着大致相同:
someFunctionCall(if (dateSentFormatted != null) dateSentFormatted else LocalDateTime.now())
安全调用运算符
?.
,仅当LHS不为null时才调用method/getter,否则直接返回null。这在遵循对象链时很有用,例如:val postcode = employee?.manager?.address?.postcode
这意味着大致相同:
val postcode = if (employee == null) null else if (employee.manager == null) null else if (employee.manager.address == null) null else employee.manager.address.postcode
…除了如果这些对象中的任何一个同时更新它不会失败。
非空断言运算符
!!
。如果此时确定该值不能为 null,则此运算符会告诉编译器您知道得更多。 (如果你错了,你会得到一个运行时异常。)例如:someFunctionCall(dateSentFormatted!!)
运行时错误的可能性意味着这很少是一个好的选择(这就是为什么他们让它看起来很可怕)。
更复杂的东西,取决于你想做什么……
方法parse(CharSequence, DateTimeFormatter)
的java.time.LocalDateTime
class不接受null
作为字符序列(第一个参数),所以你必须明确null
-检查并将函数中的 return 重写为
return if (dateEmailSent != null) LocalDateTime.parse(dateEmailSent, formatter) else null
(顺便说一句,你不必有分号(;
))
此外,您编写的函数的 return 类型不可为空,因此您必须将其更改为 LocalDateTime?
。
而且,如果在 dateEmailSent
为 null
的情况下不使用它,那么创建 parser/formatter 是没有意义的,我建议重写整个函数如下:
fun timeFormatted(dateEmailSent: String?) = if (dateEmailSent != null) {
LocalDateTime.parse(dateEmailSent, DateTimeFormatter.ofPattern("yyyy-MM-dd:HH:mm:ss"))
} else null
最后一部分由您决定,如果您想实现问题中描述的功能,其余部分几乎是强制性的。