Android difference between two dates in seconds

后端 未结 4 1386
离开以前
离开以前 2020-12-24 05:16

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

Cursor cursor = sqlite.myDataBase.rawQuery(\"SELECT StartDate, EndDate FROM Tracks          


        
4条回答
  •  [愿得一人]
    2020-12-24 06:09

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

提交回复
热议问题