工作中用到了很多的有关时间计算的工具方法,特此留念
/**
* 计算两个日期之间相差的天数
* @param smdate 较小的时间
* @param bdate 较大的时间
* @return 相差天数
* @throws ParseException
*/
public static int daysBetween(Date smdate,Date bdate){
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
try {
smdate=sdf.parse(sdf.format(smdate));
} catch (ParseException e) {
logger.error(e.getMessage(),e.fillInStackTrace());
}
try {
bdate=sdf.parse(sdf.format(bdate));
} catch (ParseException e) {
logger.error(e.getMessage(),e.fillInStackTrace());
}
Calendar cal = Calendar.getInstance();
cal.setTime(smdate);
long time1 = cal.getTimeInMillis();
cal.setTime(bdate);
long time2 = cal.getTimeInMillis();
long between_days=(time2-time1)/(1000*3600*24);
if(between_days==0)//如果小于一天按一天计算
return 1;
return Integer.parseInt(String.valueOf(between_days));
}
/**
*字符串的日期格式的计算
*/
public static int daysBetween(String smdate,String bdate){
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
Calendar cal = Calendar.getInstance();
try {
cal.setTime(sdf.parse(smdate));
} catch (ParseException e) {
logger.error(e.getMessage(),e.fillInStackTrace());
}
long time1 = cal.getTimeInMillis();
try {
cal.setTime(sdf.parse(bdate));
} catch (ParseException e) {
logger.error(e.getMessage(),e.fillInStackTrace());
}
long time2 = cal.getTimeInMillis();
long between_days=(time2-time1)/(1000*3600*24);
return Integer.parseInt(String.valueOf(between_days));
}
/**
* 根据指定日 划分时间段
* @param start
* @param end
* @param settlement
* @return
*/
public static List<Date> divideDateByMonth(Date start,Date end,Integer settlement){
List<Date> dates=new ArrayList<Date>();
Calendar calendar = Calendar.getInstance();
Date datePoint=(Date)start.clone();
dates.add(datePoint);
if(settlement!=null){
calendar.setTime(datePoint);
calendar.set(Calendar.DATE, settlement);
if(datePoint.compareTo(calendar.getTime())>0){
calendar.add(Calendar.MONTH, 1);
}
datePoint=calendar.getTime();
dates.add(datePoint);
}
while(true){
calendar.setTime(datePoint);
calendar.add(Calendar.MONTH, 1);
if(calendar.getTime().before(end)){
datePoint=(Date) calendar.getTime();
dates.add(datePoint);
}else{
break;
}
}
dates.add(end);
return dates;
}
/**
* 获得从开始日期到结束日期的 月数 和天数
* @param start
* @param end
* @return
*/
public static Integer[] getMonthAndDayNum(Date start,Date end){
Date datePoint=(Date) start.clone();
int mNumber=0;//月份数
int restDays=0;
while (datePoint.before(end)) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(datePoint);
calendar.add(Calendar.MONTH, 1);
datePoint=calendar.getTime();
if(datePoint.before(end))
mNumber++;
}
Calendar calendar = Calendar.getInstance();
calendar.setTime(datePoint);
calendar.add(Calendar.MONTH, -1);
datePoint=calendar.getTime();
restDays=daysBetween(datePoint, end);
Integer[] result= new Integer[]{mNumber,restDays};
return result;
}
/**
* 月份相等
* @param d1
* @param d2
* @return
*/
public static boolean monthEqual(Date d1,Date d2){
Calendar calendar1=Calendar.getInstance();
Calendar calendar2=Calendar.getInstance();
calendar1.setTime(d1);
calendar2.setTime(d2);
int m1=calendar1.get(Calendar.MONTH);
int m2=calendar2.get(Calendar.MONTH);
return m1==m2;
}
/**
* 月份加
* @param date
* @param amount
* @return
*/
public static Date monthAdd(Date date,int amount){
Calendar calendar=Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.MONTH, amount);
return calendar.getTime();
}
/**
* 日期加
* @param date
* @param amount
* @return
*/
public static Date dayAdd(Date date,int amount) {
Calendar calendar=Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.DATE, amount);
return calendar.getTime();
}