【问题标题】:How to check string date is in today?如何检查字符串日期在今天?
【发布时间】:2020-04-01 09:18:56
【问题描述】:

2019-12-07 20:13:04

如果这个日期在今天,我需要我的按钮可见性可见。

try {
        String dtStart = sales.getDate();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-mm-dd HH:mm:ss");
        Date date = format.parse(dtStart);


        Calendar now = Calendar.getInstance();
        Date today = now.getTime();
        if (date == today){
            holder.delete.setVisibility(View.VISIBLE);
        }else {
            holder.delete.setVisibility(View.INVISIBLE);
        }
    } catch (ParseException e) {
        e.printStackTrace();
    }

【问题讨论】:

  • String dtStart = sales.getDate(); /// 2019-12-07 20:13:04
  • 永远不要使用==来检查对象的相等性
  • 如果您使用的是java8或更高版本,则不应再使用旧的日期和日历api
  • 如果两个日期都是字符串,你必须使用 date.equals(today)
  • 您正在使用糟糕的日期时间类,这些类在几年前已被 java.time 类取代。

标签: java android if-statement


【解决方案1】:

使用 java-8 日期时间 API,首先您的格式化程序错误月份应该用大写字母 MM 表示,并使用 DateTimeFormatter 而不是旧版 SimpleDateFormat

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

然后使用格式化程序将输入字符串解析成LocalDateTime

String date = "2019-12-07 20:13:04";

LocalDateTime dateTime = LocalDateTime.parse(date,formatter);

最后使用equals比较输入日期和当前日期,here是在android上获取java-8日期时间API的信息

dateTime.toLocalDate().equals(LocalDate.now());  //true

【讨论】:

    【解决方案2】:

    您可以使用DateUtils.isToday 方法来检查日期是否是今天:

    try {
        String dtStart = sales.getDate();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-mm-dd HH:mm:ss");
        Date date = format.parse(dtStart);
    
        boolean isToday = DateUtils.isToday(date.getTime());
        if (isToday){
            holder.delete.setVisibility(View.VISIBLE);
        }else {
            holder.delete.setVisibility(View.INVISIBLE);
        }
    } catch (ParseException e) {
        e.printStackTrace();
    }
    

    【讨论】:

      【解决方案3】:

      如果您已经将日期字符串转换为日期,则可以将 Date-Object 转换为 Calendar-Object 并比较年、月和日。

      示例实现:

      private boolean isToday(Date date) {
              Calendar calendar = Calendar.getInstance();
              Calendar toCompare = Calendar.getInstance();
              toCompare.setTimeInMillis(date.getTime());
      
              return calendar.get(Calendar.YEAR) == toCompare.get(Calendar.YEAR)
                  && calendar.get(Calendar.MONTH) == toCompare.get(Calendar.MONTH)
                  && calendar.get(Calendar.DAY_OF_MONTH) == toCompare.get(Calendar.DAY_OF_MONTH);
          }
      

      或者,您可以将DateCalendar 转换为毫秒并与今天的毫秒进行比较。

      示例实现:

      private boolean isToday2(Date dateToCheck) {
              Calendar calendar = Calendar.getInstance();
      
              // you have to set calendar object to 00:00:00
              calendar.set(Calendar.HOUR, 0);
              calendar.set(Calendar.MINUTE, 0);
              calendar.set(Calendar.SECOND, 0);
      
              // milliseconds of 1 day = 86400000
              return dateToCheck.getTime() - calendar.getTimeInMillis() < 86400000; 
          }
      

      两种解决方案都没有正确处理本地化时间。所以谨慎使用。

      【讨论】:

        【解决方案4】:

        我们可以使用 Java 8 Date-Time api 如下:

            LocalDate dt2= LocalDate.of(2019,10,20);
        
            String dtStart= dt2.format(DateTimeFormatter.ISO_DATE);
        
            LocalDate currentDt= LocalDate.now();
        
            if(currentDt.format(DateTimeFormatter.ISO_DATE).equals(dtStart)) {
                // logic here 
            }else {
                //logic here
            }
        

        【讨论】:

        • 使用LocalDate 是个好主意。在比较之前进行格式化会使事情变得过于复杂。只需比较两个LocalDate 对象,如the answer by Deadpool 所示。
        猜你喜欢
        • 1970-01-01
        • 2020-11-13
        • 2011-09-26
        • 2014-09-09
        • 1970-01-01
        • 1970-01-01
        • 2014-09-13
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多