【发布时间】: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