Java TimeUnit Calculate timeDiff(Calendar end, Calendar begin, TimeUnit timeUnit)

Here you can find the source of timeDiff(Calendar end, Calendar begin, TimeUnit timeUnit)

Description

time Diff

License

Apache License

Declaration

public static long timeDiff(Calendar end, Calendar begin, TimeUnit timeUnit) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.util.Calendar;
import java.util.Date;

import java.util.concurrent.TimeUnit;

public class Main {

    public static long timeDiff(Calendar end, Calendar begin, TimeUnit timeUnit) {
        long timeDiff = 0L;
        long diffMills = end.getTime().getTime() - begin.getTime().getTime();
        switch (timeUnit) {
        case DAYS:
            timeDiff = diffMills / (1000 * 60 * 60 * 24);
            break;
        case HOURS:
            timeDiff = diffMills / (1000 * 60 * 60);
            break;
        case MINUTES:
            timeDiff = diffMills / (1000 * 60);
            break;
        case SECONDS:
            timeDiff = diffMills / (1000);
            break;
        default:/* w ww .jav  a 2s .c o m*/
            break;
        }
        return timeDiff;
    }

    public static long timeDiff(Date end, Date begin, TimeUnit timeUnit) {
        long timeDiff = 0L;
        long diffMills = end.getTime() - begin.getTime();
        switch (timeUnit) {
        case DAYS:
            timeDiff = diffMills / (1000 * 60 * 60 * 24);
            break;
        case HOURS:
            timeDiff = diffMills / (1000 * 60 * 60);
            break;
        case MINUTES:
            timeDiff = diffMills / (1000 * 60);
            break;
        case SECONDS:
            timeDiff = diffMills / (1000);
            break;
        default:
            break;
        }
        timeDiff = Math.abs(timeDiff);
        return timeDiff;
    }
}

Related

  1. sleep(long pTime, TimeUnit pTimeUnit)
  2. sleep(TimeUnit timeUnit, long duration)
  3. sleep(TimeUnit unit, long length)
  4. sleepAndCachedInterruptedException(long sleepFor, TimeUnit unit)
  5. sleepFor(long period, TimeUnit timeUnit)
  6. timeDiff(Date date1, Date date2, TimeUnit minutes)
  7. timeSince(final long startNanos, final TimeUnit destUnit)
  8. waitForEnd(Process process, long timeout, TimeUnit unit)