如何计算时间差
编写一个Java程序,通过java.util.Date获取当前的系统时间,使用String类截取时、分、秒部分输出控制台,循环一千万次后,再取得当前的系统时间截取时、分、秒部分输出到控制台。比较时间差?
package com.suanfa;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
public class Aoyun {
public static void main(String[] args) throws Exception {
GregorianCalendar gc = new GregorianCalendar();
int nowyear = gc.get(Calendar.YEAR);
int nowmonth = (gc.get(Calendar.MONTH)) + 1;
int nowdate = gc.get(Calendar.DATE);
int nowhour = gc.get(Calendar.HOUR);
int nowminute = gc.get(Calendar.MINUTE);
int nowsecond = gc.get(Calendar.SECOND);
int ap=gc.get(GregorianCalendar.AM_PM);
String ampm;
if(ap==0) ampm="上午";
else ampm="下午";
System.out.println("现在是北京时间:" + nowyear + "年" + nowmonth + "月"
+ nowdate + "日"+ampm + nowhour + "时" + nowminute + "分" + nowsecond
+ "秒");
// String strDate1 = "2008-06-2";
String strDate2 = "2008-08-8";
String strDate3 = (nowyear + "-" + nowmonth + "-" + nowdate);
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
// Date d1 = format.parse(strDate1);
Date d2 = format.parse(strDate2);
Date d3 = format.parse(strDate3);
// System.out.println("距离北京奥运会还相差:" + (d2.getTime() - d1.getTime())
// / 3600000 / 24 + "天");
System.out.println("距离北京奥运会还相差:" + (d2.getTime() - d3.getTime())
/ 3600000 / 24 + "天");
}
}
自己看吧!