【问题标题】:Android difference between two dates in seconds以秒为单位的两个日期之间的Android差异
【发布时间】:2011-04-27 00:22:59
【问题描述】:

我在网络上尝试了不同的方法,但无法成功。

Cursor cursor = sqlite.myDataBase.rawQuery("SELECT StartDate, EndDate FROM Tracks Where Id="+'"'+trackId+'"',null);

SimpleDateFormat outputFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date startDate = outputFormat.parse(cursor.getString(cursor.getColumnIndex("StartDate")));
Date endDate = outputFormat.parse(cursor.getString(cursor.getColumnIndex("EndDate")));

通过这种方式,我得到了格式良好的两个日期。现在我想在几秒钟内找出EndDateStartdate 之间的区别。

有什么建议吗?谢谢。

【问题讨论】:

    标签: android date seconds


    【解决方案1】:

    您可以将日期对象转换为 long(自 1970 年 1 月 1 日以来的毫秒数),然后使用 TimeUnit 获取秒数:

    long diffInMs = endDate.getTime() - startDate.getTime();
    
    long diffInSec = TimeUnit.MILLISECONDS.toSeconds(diffInMs);
    

    编辑: -更正了变量 diffInMs 的名称,该名称在第二行中写为 diffInM(i)s。

    【讨论】:

      【解决方案2】:

      只是为使用 Time 而不是 Date 的其他开发人员(如我)补充这个答案。

      Time t1 = new Time();
      t1.setToNow();
      Thread.sleep(1000);
      
      Time t2 = new Time();
      t2.setToNow();
      
      diff = TimeUnit.MILLISECONDS.toSeconds(t2.toMillis(true)-t1.toMillis(true));
      

      【讨论】:

      【解决方案3】:

      试试下面的方法:-

          public String twoDatesBetweenTime(String oldtime)
          {
              // TODO Auto-generated method stub
              int day = 0;
              int hh = 0;
              int mm = 0;
              try
              {
                  SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                  Date oldDate = dateFormat.parse(oldtime);
                  Date cDate = new Date();
                  Long timeDiff = cDate.getTime() - oldDate.getTime();
                  day = (int) TimeUnit.MILLISECONDS.toDays(timeDiff);
                  hh = (int) (TimeUnit.MILLISECONDS.toHours(timeDiff) - TimeUnit.DAYS.toHours(day));
                  mm = (int) (TimeUnit.MILLISECONDS.toMinutes(timeDiff) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(timeDiff)));
              }
              catch (ParseException e)
              {
                  e.printStackTrace();
              }
              if(day==0)
              {
                  return hh + " hour " + mm + " min";
              }
              else if(hh==0)
              {
                  return mm + " min";
              }
              else
              {
                  return day + " days " + hh + " hour " + mm + " min";
              }
          }
      

      【讨论】:

      • 如果我的旧日期是 2018-05-8 12:05:00 并且当前日期是 2018-05-9 12:37:00 这将失败,这不会显示日差
      • 是的,如果我的旧日期是 2018-08-30 16:34:54 并且当前日期是 2018-09-01 20:50:07,那么这将失败,那么毫秒差是负数跨度>
      【解决方案4】:

      虽然其他答案有效并且在 2010 年是很好的答案,但我提供的是现代答案。

      java.time 和 ThreeTenABP

          DateTimeFormatter formatter = DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss");
          ZoneId zone = ZoneId.systemDefault();
      
          String startDateString = "2019-12-31 23:34:45";
          String endDateString = "2020-01-01 07:56:34";
      
          ZonedDateTime start = LocalDateTime.parse(startDateString, formatter).atZone(zone);
          ZonedDateTime end = LocalDateTime.parse(endDateString, formatter).atZone(zone);
      
          long diffSeconds = ChronoUnit.SECONDS.between(start, end);
      
          System.out.println("Difference: " + diffSeconds + " seconds");
      

      在大多数时区,此 sn-p 的输出将是:

      差异:30109 秒

      我正在使用ZonedDateTime,因为它考虑了夏令时 (DST) 和其他时间异常的转换。当然,它要求您使用正确的时区。如果在您将日期和时间存储到数据库后更改了设备的时区设置,您可能会遇到一些意外。为防止这种情况发生,您可以将 UTC 偏移量与您的时间一起存储,然后在检索时将其解析为 OffsetDateTime

      问:java.time 不需要 Android API 26 级吗?

      java.time 在较旧和较新的 Android 设备上都能很好地工作。它只需要至少 Java 6

      • 在 Java 8 及更高版本以及更新的 Android 设备(从 API 级别 26 起)中,现代 API 是内置的。
      • 在非 Android 的 Java 6 和 7 中,获取 ThreeTen Backport,这是现代类的后向端口(对于 JSR 310,ThreeTen;请参阅底部的链接)。
      • 在(较旧的)Android 上使用 ThreeTen Backport 的 Android 版本。它被称为 ThreeTenABP。并确保从 org.threeten.bp 导入日期和时间类以及子包。

      链接

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-09-13
        • 1970-01-01
        • 1970-01-01
        • 2011-05-20
        • 1970-01-01
        • 2012-12-03
        • 2023-04-01
        • 1970-01-01
        相关资源
        最近更新 更多