/**
* 计算两个日期之间的天数
* @param date1 开始的日期
* @param date2 结束的日期
* @return
*/
public static int getDays(java.util.Date date1, java.util.Date date2) {
//date2应大于date1
int days = 0;
days = (int) ( (date2.getTime() - date1.getTime()) / (24 * 60 * 60 * 1000));
return days;
}
* 计算两个日期之间的天数
* @param date1 开始的日期
* @param date2 结束的日期
* @return
*/
public static int getDays(java.util.Date date1, java.util.Date date2) {
//date2应大于date1
int days = 0;
days = (int) ( (date2.getTime() - date1.getTime()) / (24 * 60 * 60 * 1000));
return days;
}
解决方案 »
- 日期问题,请大家帮忙
- hibernate的一个问题:object references an unsaved transient instance - save the transient instance before
- 配置SSH是出错了 纠结了几天 希望大家帮帮忙!
- HIBERNATE调用有两个返回值的oracle存储过程怎么做
- jfreechart做报表
- spring list页面的解决
- hibernate 中得子查询,怎么修改(多对一得关系)。请高手看看,谢谢!
- 如何把tomcat的工作目录root改成我自己的工作路径?
- 各位大侠:为什么http://ant.apache.org上不去了?
- 为啥有的方法里面throw new 自定义异常类("") 外的方法外面有throws 有的没有啊
- rmi的调用问题,大家来看看,谢谢各位高手
- 不知128M内存能不能运行weblogic server?
import java.util.*;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;public class DateDiff{
public static void main(String[] args){
String s1= "20040301";
String s2="20040330";
DateFormat df1 = new SimpleDateFormat("yyyyMMDD");
Date date1=new Date();
Date date2=new Date();
String strResult="";
Calendar d1=Calendar.getInstance();
Calendar d2=Calendar.getInstance(); try{
date1= df1.parse(s1) ;
date2= df1.parse(s2) ;
d1.setTime(date1);
d2.setTime(date2);
long result;
result=dateDiff("DAY",d1,d2);
strResult=String.valueOf(result);
}catch(ParseException e){ }
System.out.println(strResult);
}
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 start time is after the end time
if(startCal.after(endCal)){
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++;
}
} return res*reverse;
}}