提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
使用Calendar类查找日期和时间之间的间隔
import java.util.Calendar; import java.util.Date; /*来自 nowjava.com - 时代Java*/ import java.util.concurrent.TimeUnit; public class Main { public static void main(String[] args) { Calendar cal1 = Calendar.getInstance(); Calendar cal2 = Calendar.getInstance(); // Set the date to 01/01/2010:12:00 cal2.set(2010, 0, 1, 12, 0); Date date1 = cal2.getTime(); System.out.println(date1); long mill = Math.abs(cal1.getTimeInMillis() - date1.getTime()); // Convert to hours long hours = TimeUnit.MILLISECONDS.toHours(mill);/**时 代 J a v a**/ // Convert to days Long days = TimeUnit.HOURS.toDays(hours); String diff = String.format("%d hour(s) %d min(s)",hours,TimeUnit.MILLISECONDS.toMinutes(mill) - TimeUnit.HOURS.toMinutes(hours)); System.out.println(diff); diff = String.format("%d days", days); System.out.println(diff);