日期:2014-05-18 浏览次数:20899 次
/**
* 计算时间差
*
* @param beginTime
* 开始时间,格式:yyyy-MM-dd HH:mm:ss
* @param endTime
* 结束时间,格式:yyyy-MM-dd HH:mm:ss
* @return 从开始时间到结束时间之间的时间差(秒)
*/
public static long getTimeDifference(String beginTime, String endTime) {
long between = 0;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date end = null;
Date begin = null;
try {// 将截取到的时间字符串转化为时间格式的字符串
end = sdf.parse(endTime);
begin = sdf.parse(beginTime);
} catch (ParseException e) {
e.printStackTrace();
}
between = (end.getTime() - begin.getTime()) / 1000;// 除以1000是为了转换成秒
return between;
}
private static void print() {
Calendar calStart = Calendar.getInstance();
calStart.set(2013, 0, 12);
Calendar calEnd = Calendar.getInstance();
calEnd.set(2013, 5, 22);
int days = 0;
while (true) {
days++;
if (calStart.get(Calendar.DAY_OF_WEEK) == 7) {
System.out.println(calStart.get(Calendar.YEAR) + "-" + (calStart.get(Calendar.MONTH) + 1) + "-" + calStart.get(Calendar.DATE));
}
calStart.add(Calendar.DAY_OF_YEAR, 1);
if (calStart.after(calEnd)) {
break;
}
}
System.out.println("weeks : " + days / 7);
}