首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Java Web开发 >

java中怎么得到两个日期相差几天?

2012-02-03 
java中如何得到两个日期相差几天??在Java中,如何取得两个日期之间相差几天???比如我的时间a字段内容为:200

java中如何得到两个日期相差几天??
在Java中,如何取得两个日期之间相差几天???       比如我的时间a字段内容为:2005-09-03       23:15          
    时间b字段内容为:2005-09-04       01:09       如何计算他们之间相差的天数是1呢,用什么函数?注意,不是指的相差24小时的完整的天数,而是日期之差。再比如a   2005-09-28   23:15   到   b   2005-10-06   00:13   相差   8天

[解决办法]
SimpleDateFormat smdf = new SimpleDateFormat( "yyyy-MM-dd ");
try {
Date start = smdf.parse( "2005-09-28 23:15 ");
Date end = smdf.parse( "2005-10-06 00:13 ");
long t = (end.getTime() - start.getTime()) / (3600 * 24 * 1000);
System.out.println(t);
} catch (ParseException e) {
e.printStackTrace();
}
[解决办法]
首先格式化成“年-月-日”形式
SimpleDateFormat smdf = new SimpleDateFormat( "yyyy-MM-dd ");
try {
Date start = smdf.parse( "2005-09-28 23:59:59 ");
Date end = smdf.parse( "2005-09-29 00:00:00 ");
long t = (end.getTime() - start.getTime()) / (3600 * 24 * 1000);
System.out.println(t);
} catch (ParseException e) {
e.printStackTrace();
}
上述时间虽然只差1秒钟,但是按照上述方法计算却相差一天!!!

如果计算相差小时数格式化改为 "yyyy-MM-dd hh " ;计算改为:long t = (end.getTime() - start.getTime()) / (3600 * 1000);
其它可以一次类推了



[解决办法]
import java.text.SimpleDateFormat;
import java.util.Date;

import com.sun.org.apache.xerces.internal.impl.xpath.regex.ParseException;

public class Test {
public static void main(String[] args) throws Exception{
SimpleDateFormat smdf = new SimpleDateFormat( "yyyy-MM-dd hh:mm:ss ");
String result= " ";
try {
Date start = smdf.parse( "2005-09-28 23:59:59 ");
Date end = smdf.parse( "2005-09-29 00:00:00 ");
long t = (end.getTime() - start.getTime()) /1000;
if(t> =60&&t <3600){
System.out.println( "相差: "+t/60+ "分钟 ");
}else if(t> =3600&&t <(3600*24)){
System.out.println( "相差: "+t/3600+ "小时 ");
}else if(t> =(3600*24)){
System.out.println( "相差: "+t/(3600*24)+ "天 ");
}else{
System.out.println( "相差: "+t+ "秒 ");
}
} catch (ParseException e) {
e.printStackTrace();
}
}

}
[解决办法]
public static String computeDateInterval(long time1, long time2)
{
long time = time2 - time1;
time = time / 1000; // 将毫秒转化为秒

long second = time % 60; // 秒
time = time / 60;

long minute = time % 60; // 分
time = time / 60;

long hour = time % 24; // 小时
time = time / 24;

long day = time; // 天

String result = " ";

if (0 != day)
{
result += day + " days ";
}
if (0 != hour)
{
result += hour + " hours ";
}
if (0 != minute)
{
result += minute + " minutes ";
}
if (0 != second)
{
result += second + " seconds ";
}

return result;

}


[解决办法]
// total millseconds of one day
public static final long DAY_MILLSECONDS = 24 * 3600 * 1000;

/**
* Get the absolute day diff
* @return
* @param calEnd
* @param calStart
* <br/> e.g: calStart = 2005/11/01 09:00:00
* <br/> calEnd = 2005/11/06 00:00:00
* <br/> then return 5, not 4
*/
public static int getAbsDayDiff(Calendar calStart, Calendar calEnd) {
Calendar start = (Calendar)calStart.clone();
Calendar end = (Calendar)calEnd.clone();

start.set(start.get(Calendar.YEAR), start.get(Calendar.MONTH), start.get(Calendar.DATE), 0, 0, 0);
start.set(Calendar.MILLISECOND, 0);

end.set(end.get(Calendar.YEAR), end.get(Calendar.MONTH), end.get(Calendar.DATE), 0, 0, 0);
end.set(Calendar.MILLISECOND, 0);

return (int)((end.getTimeInMillis() - start.getTimeInMillis()) / DAY_MILLSECONDS);
}

热点排行