Here you can find the source of timeDiff(Calendar end, Calendar begin, TimeUnit timeUnit)
public static long timeDiff(Calendar end, Calendar begin, TimeUnit timeUnit)
//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; } }