Getting difference between two dates Android

后端 未结 6 1808
故里飘歌
故里飘歌 2020-12-01 22:05

Am having string posting date like :

2011-03-27T09:39:01.607

and There is current date .

I want to get difference between these tw

6条回答
  •  无人及你
    2020-12-01 22:54

    Convert both dates into calender and make time 0(

    today.set(Calendar.HOUR_OF_DAY, 0);
        today.set(Calendar.MINUTE, 0);
        today.set(Calendar.SECOND, 0);
    

    ).
    Then use this fun :

    public final static long SECOND_MILLIS = 1000;
    public final static long MINUTE_MILLIS = SECOND_MILLIS*60;
    public final static long HOUR_MILLIS = MINUTE_MILLIS*60;
    public final static long DAY_MILLIS = HOUR_MILLIS*24;
    
     public static int daysDiff( Date earlierDate, Date laterDate )
        {
            if( earlierDate == null || laterDate == null ) return 0;
            return (int)((laterDate.getTime()/DAY_MILLIS) - (earlierDate.getTime()/DAY_MILLIS));
        }
    

提交回复
热议问题