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));
startCal.add(Calendar.MILLISECOND,tmp);
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);
startCal.add(Calendar.MILLISECOND,tmp);
if(startCal.get(type)!=cur){
res++;
}
}
/*
* @(#)DateDiff.java 1.00 11/26/2003
*
*/
import java.util.Calendar;
/**
*
*
* Copyright 2003 Wobelisk. All rights reserved.
* 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);