本示例演示如何获取日期差值, 如 0 day 0 hour 0 min 1 sec
package c1;
import java.util.Calendar;
public class Test1 {
public static void main(String[] args) {
Calendar calendar = Calendar.getInstance();
int oday=calendar.get(Calendar.DAY_OF_MONTH);
int ohour=calendar.get(Calendar.HOUR_OF_DAY);
int omin=calendar.get(Calendar.MINUTE);
int osec=calendar.get(Calendar.SECOND);
System.out.println(oday);
System.out.println(ohour);
System.out.println(omin);
System.out.println(osec);
try{Thread.sleep(1000);
}
catch(Exception e){}
Calendar ccalendar = Calendar.getInstance();
int cday=ccalendar.get(Calendar.DAY_OF_MONTH);
int chour=ccalendar.get(Calendar.HOUR_OF_DAY);
int cmin=ccalendar.get(Calendar.MINUTE);
int csec=ccalendar.get(Calendar.SECOND);
System.out.println(cday);
System.out.println(chour);
System.out.println(cmin);
System.out.println(csec);
if(csec-osec<0){
csec=csec+60;
cmin=cmin-1;
}
if(cmin-omin<0){
cmin=cmin+60;
chour=chour-1;
}
if(chour-ohour<0){
chour=chour+24;
cday=cday-1;
}
System.out.println((cday-oday)+" day "+(chour-ohour)+" hour " + (cmin-omin)+" min "+(csec-osec)+" sec");
}
}
/*
输出结果:
19
11
40
9
19
11
40
10
0 day 0 hour 0 min 1 sec
*/
阅读(803) | 评论(0) | 转发(0) |