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

后端 未结 11 2260
天命终不由人
天命终不由人 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:37

    You're close to the right answer, you are getting the difference in milliseconds between those two dates, but when you attempt to construct a date out of that difference, it is assuming you want to create a new Date object with that difference value as its epoch time. If you're looking for a time in hours, then you would simply need to do some basic arithmetic on that diff to get the different time parts.

    Java:

    long diff = date1.getTime() - date2.getTime();
    long seconds = diff / 1000;
    long minutes = seconds / 60;
    long hours = minutes / 60;
    long days = hours / 24;
    

    Kotlin:

    val diff: Long = date1.getTime() - date2.getTime()
    val seconds = diff / 1000
    val minutes = seconds / 60
    val hours = minutes / 60
    val days = hours / 24
    

    All of this math will simply do integer arithmetic, so it will truncate any decimal points

    0 讨论(0)
  • 2020-11-28 09:40

    If you use Kotlin language for Android development, you can use ExperimentalTime extension. To get difference in days you can use it like this:

    @ExperimentalTime
    fun daysDiff(c1: Calendar, c2: Calendar): Double {
        val diffInMillis = c1.timeInMillis - c2.timeInMillis
        return diffInMillis.milliseconds.inDays
    }
    

    or if you want to get results as integer:

    @ExperimentalTime
    fun daysDiff2(c1: Calendar, c2: Calendar): Int {
        val diffInMillis = c1.timeInMillis - c2.timeInMillis
        return diffInMillis.milliseconds.toInt(DurationUnit.DAYS)
    }
    
    0 讨论(0)
  • 2020-11-28 09:43

    Here is my answer based on @Ole V. V. answer.

    This also works with Singular.

    private String getDuration(Date d1, Date d2) {
        Duration diff = Duration.between(d1.toInstant(), d2.toInstant());
    
    
        long days = diff.toDays();
        diff = diff.minusDays(days);
        long hours = diff.toHours();
        diff = diff.minusHours(hours);
        long minutes = diff.toMinutes();
        diff = diff.minusMinutes(minutes);
        long seconds = diff.toMillis();
    
        StringBuilder formattedDiff = new StringBuilder();
        if(days!=0){
            if(days==1){
                formattedDiff.append(days + " Day ");
    
            }else {
                formattedDiff.append(days + " Days ");
            }
        }if(hours!=0){
            if(hours==1){
                formattedDiff.append(hours + " hour ");
            }else{
                formattedDiff.append(hours + " hours ");
            }
        }if(minutes!=0){
            if(minutes==1){
                formattedDiff.append(minutes + " minute ");
            }else{
                formattedDiff.append(minutes + " minutes ");
            }
        }if(seconds!=0){
            if(seconds==1){
                formattedDiff.append(seconds + " second ");
            }else{
                formattedDiff.append(seconds + " seconds ");
            }
        }
    
    
        return formattedDiff.toString();
    }
    

    It works with a StringBuilder to append everything together.

    0 讨论(0)
  • 2020-11-28 09:44

    Using georgian calander

     public void dateDifferenceExample() {
    
            // Set the date for both of the calendar instance
            GregorianCalendar calDate = new GregorianCalendar(2012, 10, 02,5,23,43);
            GregorianCalendar cal2 = new GregorianCalendar(2015, 04, 02);
    
            // Get the represented date in milliseconds
            long millis1 = calDate.getTimeInMillis();
            long millis2 = cal2.getTimeInMillis();
    
            // Calculate difference in milliseconds
            long diff = millis2 - millis1;
    
            // Calculate difference in seconds
            long diffSeconds = diff / 1000;
    
            // Calculate difference in minutes
            long diffMinutes = diff / (60 * 1000);
    
            // Calculate difference in hours
            long diffHours = diff / (60 * 60 * 1000);
    
            // Calculate difference in days
            long diffDays = diff / (24 * 60 * 60 * 1000);
        Toast.makeText(getContext(), ""+diffSeconds, Toast.LENGTH_SHORT).show();
    
    
    }
    
    0 讨论(0)
  • 2020-11-28 09:46

    I tried this method..but don't know why I am not getting proper result

    long diff = date1.getTime() - date2.getTime();
    long seconds = diff / 1000;
    long minutes = seconds / 60;
    long hours = minutes / 60;
    long days = hours / 24;
    

    But this works

    long miliSeconds = date1.getTime() -date2.getTime();
    long seconds = TimeUnit.MILLISECONDS.toSeconds(miliSeconds);
    long minute = seconds/60;
    long hour = minute/60;
    long days = hour/24;
    
    0 讨论(0)
  • 2020-11-28 09:49
        long diffInMillisec = date1.getTime() - date2.getTime();
    
        long diffInDays = TimeUnit.MILLISECONDS.toDays(diffInMillisec);
        long diffInHours = TimeUnit.MILLISECONDS.toHours(diffInMillisec);
        long diffInMin = TimeUnit.MILLISECONDS.toMinutes(diffInMillisec);
        long diffInSec = TimeUnit.MILLISECONDS.toSeconds(diffInMillisec);
    
    0 讨论(0)
提交回复
热议问题