【问题标题】:timestamp null value from cassandra checking failed in java来自 cassandra 检查的时间戳空值在 java 中失败
【发布时间】:2019-06-29 02:20:53
【问题描述】:

在我的表单中,我没有选择日期,将该字段作为空传递,在数据库中它存储为空。在编辑期间,我正在检查该时间戳值是否为空,而打印该值为空。但是在使用 if 条件检查时它失败了。

为此,如果条件失败,我将时间戳与 null 和长度 ==0 进行比较。

if(session.getAttribute("deliveryDate").toString()!=null){ 
   String delidate = session.getAttribute("deliveryDate").toString();

     ///////////////date convertion/////////////////
     long unixSecondsfrom = Long.parseLong(delidate); 
      // convert seconds to milliseconds
      Date datefrom = new java.util.Date(unixSecondsfrom); 
      // the format of your date
      SimpleDateFormat sdffrom = new java.text.SimpleDateFormat("yyyy-MM-dd"); 
      String formattedFromDate = sdffrom.format(datefrom);
      ///////////////date convertion/////////////////
      page.set("deliveryDate", formattedFromDate);           

 }else{ page.set("deliveryDate", ""); }

当时间戳为空时,我需要上述条件为假,并且需要转到其他部分。

【问题讨论】:

  • toString 绝不能返回null
  • 如果条件失败究竟是什么意思?它是否进入else 部分,是否进入错误的部分,是否引发异常,是否出现编译错误,还是什么?
  • 不,它不属于其他部分。我试过没有 toString()。

标签: java cassandra timestamp


【解决方案1】:

if 语句有一个转换为字符串,将在比较之前评估它是否为空值,以及双重否定条件;你可以解决它:

if(session.getAttribute("deliveryDate")!=null) { 
    ...

【讨论】:

    猜你喜欢
    • 2018-06-22
    • 2017-03-30
    • 2020-01-06
    • 1970-01-01
    • 2011-04-09
    • 2022-01-16
    • 1970-01-01
    • 2017-04-30
    • 2015-03-28
    相关资源
    最近更新 更多