代码之家  ›  专栏  ›  技术社区  ›  Alin

两个日期之间的安卓差异(秒)

  •  33
  • Alin  · 技术社区  · 14 年前

    我在网上尝试过不同的方法,但都没成功。

    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")));
    

    这样我就可以得到两个格式都很好的日期。现在我想找出 EndDate Startdate 几秒钟后。

    有什么建议吗?谢谢您。

    3 回复  |  直到 8 年前
        1
  •  107
  •   Mohamed Lebon Cheryl Simon    10 年前

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

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

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

        2
  •  5
  •   duggu Neeraj Nama    10 年前

        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";
            }
        }
    
        3
  •  4
  •   António Almeida Michal Drozd    10 年前

    只是对其他使用时间而不是日期的开发人员(比如我)补充一下这个答案。

    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));
    
        4
  •  0
  •   Anonymous    5 年前

    当其他答案在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");
    

    在大多数时区中,此片段的输出将是:

    差:30109秒

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

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

    • 在非Android Java 6和7中,获取ThreeTen Backport,现代类的Backport(JSR 310的ThreeTen;请参阅底部的链接)。
    • org.threeten.bp 有分装的。

    链接