提供一个比较笨的方法,呵呵
public class TestException {
public long countTimeDifference (Timestamp minuend, Timestamp subtrahend) {
if (minuend == null || subtrahend == null)
return -1;
long date1 = minuend.getTime();
long date2 = subtrahend.getTime(); return ((date1 - date2)/60/60/24/1000);
} public static void main (String[] args) {
TestException te = new TestException(); String time1 = "2004-04-01 00:00:00";
String time2 = "2003-03-31 00:00:00";
Timestamp date1 = Timestamp.valueOf(time1);
Timestamp date2 = Timestamp.valueOf(time2); long count = te.countTimeDifference(date1, date2);
if (count != -1)
System.out.println(count);
}
}
public class TestException {
public long countTimeDifference (Timestamp minuend, Timestamp subtrahend) {
if (minuend == null || subtrahend == null)
return -1;
long date1 = minuend.getTime();
long date2 = subtrahend.getTime(); return ((date1 - date2)/60/60/24/1000);
} public static void main (String[] args) {
TestException te = new TestException(); String time1 = "2004-04-01 00:00:00";
String time2 = "2003-03-31 00:00:00";
Timestamp date1 = Timestamp.valueOf(time1);
Timestamp date2 = Timestamp.valueOf(time2); long count = te.countTimeDifference(date1, date2);
if (count != -1)
System.out.println(count);
}
}
不过可以试试笨办法:
比如:
//今天的日期
Date today=new Date();
long todayMilliseconds=today.getTime();
//2000年1月1日的日期
Date compareDay;
long compareMilliseconds=compareDay.getTime();
//获得两个日期之间的毫秒差。
long differenceMilliseconds=todayMilliseconds-compareMilliseconds;
//一天的毫秒数
long oneDayMilliseconds=24*60*60*1000;
//除以一天的毫秒数,就是相差的天数。
long result=differenceMilliseconds/oneDayMilliseconds;
Calendar c1=Calendar.getInstance();
c1.set(2000,0,1);//设置日期为2000年1月1号,月以0开始的
System.out.println(c1.getTime());
Calendar c2=Calendar.getInstance();
System.out.println(c2.getTime());
long t1=c2.getTimeInMillis()-c1.getTimeInMillis();
long t2=1000*60*60*24;
int days=(int)(t1/t2);//相差的天数
Date date1 = new Date(104, 1, 11); //2004年2月11日
Date date2 = new Date(104, 4, 1); //2004年4月1日
long x=date1.getTime();
long y=date2.getTime();
int days = (int) ((y-x)/(24*60*60*1000));
System.out.println("----------------------" + days);
}
Date date1 = new Date(100, 0, 1); //2000年1月1日
Date date2 = new Date(104, 4, 1); //2004年4月1日
long x=date1.getTime();
long y=date2.getTime();
int days = (int) ((y-x)/(24*60*60*1000));
System.out.println("----------------------" + days);
}
var miStart = Date.parse(asStartDate.replace(/\-/g, '/'));
var miEnd = Date.parse(asEndDate.replace(/\-/g, '/'));
return (miEnd-miStart)/(1000*24*3600);
}
static public String getMACAddress()throws IOException
{
Process proc = Runtime.getRuntime().exec("ipconfig -all");
InputStream istr = proc.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(istr));
String str="";
int n;
while ((str = br.readLine()) != null){
if((n=str.indexOf(":"))>0){
str=str.substring(n+1).trim();
if(str.length()==17&&str.charAt(2)=='-'){
return str;
}
}
}
return str;
}