【问题标题】:Checking if LocalDateTIme is null in a conditional检查 LocalDateTIme 在条件中是否为空
【发布时间】:2023-03-18 05:09:01
【问题描述】:

这里我有一个函数可以将字符串格式化为 LocalDateTime 并返回它。

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 != null的时候想说什么?也许是!(dateSentFormatted != null)(用== 更容易写),还是别的什么? ((!dateSentFormatted) != null 将无效,因为 !dateSentFormatted 只能是 boolean,并且永远不会为空)。

标签: java if-statement kotlin logic java-time


【解决方案1】:

java.time.LocalDateTime 类的方法parse(CharSequence, DateTimeFormatter) 不接受null 作为字符序列(第一个参数),因此您必须进行明确的null-check 并将函数的返回重写为

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

(顺便说一句,你不必有分号 (;))

此外,您编写的函数的返回类型不可为空,因此您必须将其更改为 LocalDateTime?

而且,如果在 dateEmailSentnull 的情况下不使用解析器/格式化程序,那么创建解析器/格式化程序是没有意义的,我建议将整个函数重写如下:

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

最后一部分由您决定,如果您想实现您在问题中描述的功能,其余部分几乎是强制性的。

【讨论】:

  • 谢谢你,这是我正在寻找的澄清。
【解决方案2】:

假设dateSentFormattedLocalDateTime类型的变量,你可以检查它是否为null:

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

你不需要第一个!

【讨论】:

    【解决方案3】:

    正如所写,dateSentFormatted 不能为空:您将其设置为从timeFormatted() 返回,它返回一个不可为空的LocalDateTime。 (与 LocalDateTime? 不同。)

    而且因为你没有指定它的类型,编译器推断LocalDateTime(所以即使它是var,它也永远不会为空)。

    因此,正如所写,检查 null 是没有意义的,您的 IDE 会警告您检查没有意义!

    但是,如果它可以为空的,那么有多种用于空检查的选项,具体取决于您要对其执行的操作:

    • 一个简单的if (dateSentFormatted != null) 测试,根据您的代码(但没有无关的!)。这是传统的方式,有时它仍然是最清晰的。

    • 其他if () 测试。 Kotlin 的标准库提供了一些方法来使 null 检查更简洁和/或更具可读性,例如:

      if (someString.isNullOrEmpty())
          // …
      
    • elvis 运算符?:,如果它不为空,则生成其左侧,否则生成右侧。如果未指定,这对于提供默认/备用值很有用,例如:

      someFunctionCall(dateSentFormatted ?: LocalDateTime.now())
      

      意思大致相同:

      someFunctionCall(if (dateSentFormatted != null) dateSentFormatted else LocalDateTime.now())
      
    • 安全调用运算符?.,仅当 LHS 不为 null 时才调用方法/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!!)
      

      运行时错误的可能性意味着这很少是一个好的选择(这就是他们让它看起来很可怕的原因)。

    • 更复杂的事情,取决于你想要做什么……

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-09-01
      • 2018-04-07
      • 2013-03-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多