两个日期的Android差异,以秒为单位

时间:2020-12-04 21:30:13

I've tried different methods around the web but couldn't make it work.

我在网上尝试了不同的方法,但无法使其正常工作。

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

In this way I get both dates in good format. Now I want to find the difference between EndDate and Startdate in seconds.

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

Any advice? Thank you.

任何建议?谢谢。

3 个解决方案

#1


90  

You can turn a date object into a long (milliseconds since Jan 1, 1970), and then use TimeUnit to get the number of seconds:

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

long diffInMs = endDate.getTime() - startDate.getTime();

long diffInSec = TimeUnit.MILLISECONDS.toSeconds(diffInMs);

Edit: -Corrected the name of the variable diffInMs which was written diffInM(i)s in the second line.

编辑:-Corrected在第二行写入diffInM(i)的变量diffInMs的名称。

#2


5  

try below method:-

尝试以下方法: -

    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  

Just complementing this answer for other developers (like me) who are using Time instead of Date.

只是为使用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));

#1


90  

You can turn a date object into a long (milliseconds since Jan 1, 1970), and then use TimeUnit to get the number of seconds:

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

long diffInMs = endDate.getTime() - startDate.getTime();

long diffInSec = TimeUnit.MILLISECONDS.toSeconds(diffInMs);

Edit: -Corrected the name of the variable diffInMs which was written diffInM(i)s in the second line.

编辑:-Corrected在第二行写入diffInM(i)的变量diffInMs的名称。

#2


5  

try below method:-

尝试以下方法: -

    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  

Just complementing this answer for other developers (like me) who are using Time instead of Date.

只是为使用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));