How do I get difference between two dates in android?, tried every thing and post

后端 未结 11 2288
天命终不由人
天命终不由人 2020-11-28 09:06

I saw all the post in here and still I can\'t figure how do get difference between two android dates.

This is what I do:

long diff = date1.getTime()          


        
11条回答
  •  臣服心动
    2020-11-28 09:56

    shortest answer that works for me. send start and end date in millisecond.

    public int GetDifference(long start,long end){
        Calendar cal = Calendar.getInstance();
        cal.setTimeInMillis(start);
        int hour = cal.get(Calendar.HOUR_OF_DAY);
        int min = cal.get(Calendar.MINUTE);
        long t=(23-hour)*3600000+(59-min)*60000;
    
        t=start+t;
    
        int diff=0;
        if(end>t){
            diff=(int)((end-t)/ TimeUnit.DAYS.toMillis(1))+1;
        }
    
        return  diff;
    }
    

提交回复
热议问题