# 求教关于计算时间差，多谢

bsd 2003-11-25 01:33:14

"2003/11/23 23:00:00"与"2003/11/24 01:00:00"之间相差
2个小时，1天，一个星期...

...全文
59 32 点赞 打赏 收藏 举报

32 条回复

zhang21cnboy 2003-11-28

• 打赏
• 举报

thuers 2003-11-28

• 打赏
• 举报

haoren0625 2003-11-28

• 打赏
• 举报

bsd 2003-11-27

public static long dateDiff(String part, Calendar startCal, Calendar endCal) {
long deltaMs = endCal.getTimeInMillis() - startCal.getTimeInMillis();

if ("MILLISECOND".equalsIgnoreCase(part)) {
return deltaMs;
}

int reverse = 1;
if(startCal.after(endCal)){ //if start time is after the end time
reverse = -1;
deltaMs = -deltaMs;
Calendar tmp = startCal;
startCal = endCal;
endCal = tmp;
}

long res = 0;

if ("YEAR".equalsIgnoreCase(part)) {
res = endCal.get(Calendar.YEAR) - startCal.get(Calendar.YEAR);
} else if ("MONTH".equalsIgnoreCase(part)) {
int year = endCal.get(Calendar.YEAR) - startCal.get(Calendar.YEAR);
res += year * 12;
res += endCal.get(Calendar.MONTH) - startCal.get(Calendar.MONTH);
} else if ("WEEK".equalsIgnoreCase(part)) {
res += deltaMs / (7 * 24 * 3600 * 1000);
int w = startCal.get(Calendar.DAY_OF_WEEK);
int tmp = (int)(deltaMs % (7*24*3600*1000));
int w2 = startCal.get(Calendar.DAY_OF_WEEK);
if(w2<w || (w2 == w && tmp>(24*3600*1000))){
res++;
}
} else{
long base = 0;
int type = 0;
if ("DAY".equalsIgnoreCase(part)) {
type = Calendar.DATE;
base = 24 * 3600 * 1000;
} else if ("HOUR".equalsIgnoreCase(part)) {
type = Calendar.HOUR;
base = 3600 * 1000;
} else if ("MINUTE".equalsIgnoreCase(part)) {
type = Calendar.MINUTE;
base = 60 * 1000;
} else if ("SECOND".equalsIgnoreCase(part)) {
type = Calendar.SECOND;
base = 1000;
}else{
return Long.MIN_VALUE;
}
int cur = startCal.get(type);
res = deltaMs / base;
int tmp = (int)(deltaMs % base);
if(startCal.get(type)!=cur){
res++;
}
}

return res*reverse;
}
• 打赏
• 举报

zjq1980 2003-11-27

• 打赏
• 举报

zjq1980 2003-11-27

• 打赏
• 举报

zjq1980 2003-11-27

• 打赏
• 举报

wobelisk 2003-11-27

DateDiff.dateDiff("day",c1,c2)；

• 打赏
• 举报

wobelisk 2003-11-27

/*
* @(#)DateDiff.java 1.00 11/26/2003
*
*/
import java.util.Calendar;
/**
*
*
* Free Use. No response for damage caused by errors of this file.
* <p>
* dateDiff() function similar to ms sqlserver. Calculate difference between two
* Calendars. Difference can be represented by day,week,hour,minute and second.
*
* <p>
* The difference between "2003-11-22 23:59:59" and "2003-11-23 00:00:00" is
* 1 day, or 1 week, or 1 hour, or 1 minute or 1 second
* <p>
* Default week difference is based on the rule that Sunday as the first day of
* the week. In France or China where Monday is the first day of week, you
* should set the first day of week to Monday before calculation. For Example,
* <pre>
* Calendar c1=Calendar.getInstance();
* Calendar c2=Calendar.getInstance();
* c1.set(2003,10,23,23,0,0); //Sunday 2003-11-23 23:00:00
* c2.set(2003,10,24,1,0,0); //Monday 2003-11-24 01:00:00
* dateDiff("week",c1,c2); //week difference between c1 and c2 is 0 week
* c1.setFirstDayOfWeek(Calendar.MONDAY);
* c2.setFirstDayOfWeek(Calendar.MONDAY);
* dateDiff("week",c1,c2); //week difference between c1 and c2 is 1 week
* </pre>
*
* setFirstDayOfWeek() only affects week difference
*
* @author Wobelisk@163.com
* @version 1.00 11/26/2003
*
*/

public class DateDiff{
/**
* Calculate Calendar c2 - Calendar c1
* @param s the String represents type of difference: day, week, hour,
* minute, second. String is case-insensitive
* @param c1 the Calendar instance
* @param c2 the Calendar instance
*/

public static long dateDiff(String s,Calendar c1, Calendar c2){
String s1=s.toUpperCase();
if(s1.equals("DAY")){
Calendar c11=Calendar.getInstance();
Calendar c21=Calendar.getInstance();
c11.set(c1.get(Calendar.YEAR),c1.get(Calendar.MONTH),c1.get(Calendar.DAY_OF_MONTH),0,0,0);
c21.set(c2.get(Calendar.YEAR),c2.get(Calendar.MONTH),c2.get(Calendar.DAY_OF_MONTH),0,0,0);
return (c21.getTimeInMillis()-c11.getTimeInMillis())/(1000*24*3600);
} else if(s1.equals("WEEK")){
long base=dateDiff("DAY",c1,c2);
int dw1=c1.get(Calendar.DAY_OF_WEEK);
int dw2=c2.get(Calendar.DAY_OF_WEEK);

if(c1.getFirstDayOfWeek()==Calendar.MONDAY && dw1==Calendar.SUNDAY)
dw1+=7;
if(c2.getFirstDayOfWeek()==Calendar.MONDAY && dw2==Calendar.SUNDAY)
dw2+=7;
if(base>0 && dw2<dw1)
return base/7+1;
else if(base<0 && dw2>dw1)
return base/7-1;
else
return base/7;
} else if(s1.equals("HOUR")){
return dateDiff("DAY",c1,c2)*24+c2.get(Calendar.HOUR_OF_DAY)-c1.get(Calendar.HOUR_OF_DAY);
} else if(s1.equals("MINUTE")){
return dateDiff("HOUR",c1,c2)*60+c2.get(Calendar.MINUTE)-c1.get (Calendar.MINUTE);
} else if(s1.equals("SECOND")){
return dateDiff("MINUTE",c1,c2)*60+c2.get(Calendar.SECOND)-c1.get(Calendar.SECOND);
} else
return Long.MIN_VALUE; //error
}
}
• 打赏
• 举报

zjq1980 2003-11-26

• 打赏
• 举报

bsd 2003-11-26
to wobelisk() ，

return (int)
Math.ceil((Math.abs(c1.getTimeInMillis()-c2.getTimeInMillis()))/(1000*24*3600.0));

2003/11/25 00:00:01 跟 2004/11/26 23:59:59

• 打赏
• 举报

zjq1980 2003-11-26
to wobelisk() ，

• 打赏
• 举报

wobelisk 2003-11-26
2003/11/25 00:00:01 跟 2003/11/26 23:59:59:

my output is
1 day
47 hours
2879 minutes
172798 seconds

• 打赏
• 举报

bsd 2003-11-26
to wobelisk() ，

2003/11/25 00:00:01 跟 2003/11/26 23:59:59

• 打赏
• 举报

wobelisk 2003-11-26
public int dateDiff(String s,Calendar c1, Calendar c2){
String s1=s.toUpperCase();
if(s1.equals("DAY")){
if(c1.get(Calendar.YEAR)==c2.get(Calendar.YEAR))
return c1.get(Calendar.DAY_OF_YEAR)-c2.get(Calendar.DAY_OF_YEAR);
else
return (int)
Math.ceil((Math.abs(c1.getTimeInMillis()-c2.getTimeInMillis()))/(1000*24*3600.0));
} else if(s1.equals("HOUR")){

return dateDiff("DAY",c1,c2)*24+c1.get(Calendar.HOUR_OF_DAY)-c2.get(Calendar.HOUR_OF_DAY);
//return c1.get(Calendar.HOUR_OF_DAY)-c2.get(Calendar.HOUR_OF_DAY);
} else if(s1.equals("MINUTE")){
return dateDiff("HOUR",c1,c2)*60+c1.get(Calendar.MINUTE)-c2.get(Calendar.MINUTE);
} else if(s1.equals("SECONDS")){
return dateDiff("MINUTE",c1,c2)*60+c1.get(Calendar.SECOND)-c2.get(Calendar.SECOND);
} else
return Integer.MIN_VALUE; //error
}
• 打赏
• 举报

bsd 2003-11-26

• 打赏
• 举报

zjq1980 2003-11-26

TO wobelisk() ：
8点和9点不是相差1小时吗？
• 打赏
• 举报

leshui 2003-11-26
• 打赏
• 举报

wobelisk 2003-11-26

//9:00:00 - 8:59:59 = 1second= 1小时

//9:59:59-8:00:01 = 1hour59minutes58seconds= (you also want 1小时)

what do you really want?

• 打赏
• 举报

zjq1980 2003-11-26

• 打赏
• 举报

Java SE

6.2w+

Java 2 Standard Edition

2003-11-25 01:33