两个日期之间的间隔怎么算?

ifirefox 2005-11-03 10:43:56
两个日期,精确到年月日,
现在要判断两日期之间是不是>=6年和<=3个月、
怎么得到??
有好方法吗?
...全文
697 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
数据娃掘 2005-11-10
  • 打赏
  • 举报
回复
final long DAY = 3600*1000*24;
DateFormat f = new SimpleDateFormat("yyyy-MM-dd");
Date d1 = f.parse(f.format(new Date()));
Date d2 = f.parse("2005-11-1");
System.out.println((d1.getTime()-d2.getTime())/DAY);
dinho 2005-11-09
  • 打赏
  • 举报
回复
希望高手门帮帮忙。
http://community.csdn.net/Expert/topic/4368/4368711.xml?temp=.9411127
dinho 2005-11-09
  • 打赏
  • 举报
回复
也帮我看看这个问题可以吗?
xuender(徐强)大哥,你的方法我有去试,但不行啊。不知道是不是我写的SQL语句有错。
http://community.csdn.net/Expert/topic/4368/4368711.xml?temp=.9411127
classjava 2005-11-08
  • 打赏
  • 举报
回复
public static int dateDiff(String interval,java.util.Date date1, java.util.Datedate2) {
int intReturn=-1000000000 ;
if (date1==null || date2==null || interval==null) return intReturn ;
try {
java.util.Calendar cal1 = java.util.Calendar.getInstance();
java.util.Calendar cal2 = java.util.Calendar.getInstance();

// different date might have different offset
cal1.setTime(date1);
long ldate1 = date1.getTime() + cal1.get(java.util.Calendar.ZONE_OFFSET) + cal1.get(java.util.Calendar.DST_OFFSET);

cal2.setTime(date2);
long ldate2 = date2.getTime() + cal2.get(java.util.Calendar.ZONE_OFFSET) + cal2.get(java.util.Calendar.DST_OFFSET);

// Use integer calculation, truncate the decimals
int hr1 = (int)(ldate1/3600000);
int hr2 = (int)(ldate2/3600000);

int days1 = (int)hr1/24;
int days2 = (int)hr2/24;

int yearDiff = cal2.get(java.util.Calendar.YEAR) - cal1.get(java.util.Calendar.YEAR);
int monthDiff = yearDiff * 12 + cal2.get(java.util.Calendar.MONTH) - cal1.get(java.util.Calendar.MONTH);
int dateDiff = days2 - days1;
int hourDiff = hr2 - hr1;
int minuteDiff = (int)(ldate2/60000) - (int)(ldate1/60000);
int secondDiff = (int)(ldate2/1000) - (int)(ldate1/1000);

if (interval.equals("Y")){
intReturn = yearDiff;
}
else if (interval.equals("M")){
intReturn = monthDiff;
}
else if (interval.equals("D")){
intReturn = dateDiff;
}
else if (interval.equals("H")){
intReturn = hourDiff;
}
else if (interval.equals("m")){
intReturn = minuteDiff;
}
else if (interval.equals("S")){
intReturn = secondDiff;
}
}
catch (Exception ex) {
}
return intReturn;
}
amu0528 2005-11-08
  • 打赏
  • 举报
回复
都转化为毫秒 然后相减,下面是刚写的例子

import java.util.Date;
import java.text.*;


/**
* @author Administrator
*
* TODO 要更改此生成的类型注释的模板,请转至
* 窗口 - 首选项 - Java - 代码样式 - 代码模板
*/
public class TestMinus {

public static void main(String[] args) {
Date t1 =null;
Date t2 =null;
SimpleDateFormat format = new SimpleDateFormat("yyyyMMddhhmmss");
try{ t1 = format.parse("20050927104653");}
catch(Exception o){}
try{ t2 = format.parse("20050927104652");}
catch(Exception o){}
long h = (t1.getTime()-t2.getTime())/(60*60*1000);
long s = (t1.getTime()-t2.getTime())/(60*1000)-h*60;



System.out.println("相差"+h+"小时"+s+"分钟");

}

}
xuender 2005-11-04
  • 打赏
  • 举报
回复
不要误会我说的2楼是liu_you(滴水藏海)
fbtdjs 2005-11-04
  • 打赏
  • 举报
回复
guanzhu...
yanxiazhiqiu 2005-11-04
  • 打赏
  • 举报
回复
两个都没错的。
believefym 2005-11-04
  • 打赏
  • 举报
回复
final long DAY = 3600*1000*24;
DateFormat f = new SimpleDateFormat("yyyy-MM-dd");
Date d1 = f.parse(f.format(new Date()));
Date d2 = f.parse("2005-11-1");

System.out.println((d1.getTime()-d2.getTime())/DAY);
----------------
这个只用来求两个日期之间差多少天
final long DAY = 3600*1000*24;是一天的milliseconds
cuilichen 2005-11-04
  • 打赏
  • 举报
回复
建议使用一楼的方法
xuender 2005-11-04
  • 打赏
  • 举报
回复
Calendar的getActualMaximum(Calendar.DAY_OF_MONTH)等同与
getDayOfMonth(int year,int month)

myhotsun 2005-11-04
  • 打赏
  • 举报
回复
同意二楼的
ASPGuy 2005-11-04
  • 打赏
  • 举报
回复
package djf.tool;
import java.util.Date;
import java.util.Calendar;
import java.text.DateFormat;
public class myDate{
public static String dateFormat(Date d,int f)
{String temp="";
if (f==0) {temp=String.valueOf(d);}
if (f==1) {temp=(1900+d.getYear())+"-"+(d.getMonth()+1)+"-"+d.getDate();}
if (f==2) {temp=(1900+d.getYear())+"-"+(d.getMonth()+1);}
if (f==3) {temp=(d.getMonth()+1)+"-"+d.getDate();}
if (f==4) {temp=(1900+d.getYear())+"年"+(d.getMonth()+1)+"月";}
if (f==5) {temp=(1900+d.getYear())+"-"+(d.getMonth()+1)+"-"+d.getDate()+"-"+d.getHours()+"-"+d.getMinutes()+"-"+d.getSeconds();}
return temp;}
public static Date getDateFormat(String str)
{Date d=new Date();
String[] arr=str.split("-");
d.setYear(Integer.parseInt(arr[0])-1900);
d.setMonth(Integer.parseInt(arr[1])-1);
d.setDate(Integer.parseInt(arr[2]));
return d;}
public static int dateDiff(Date d1,Date d2,int f)
{long diff=d2.getTime()-d1.getTime();
diff=diff/1000/60/60/24;
int rtn=0;
if (f==0) rtn=(int)diff;
if (f==1) rtn=(d2.getYear()*12+d2.getMonth())-(d1.getYear()*12+d1.getMonth());
if (f==2) rtn=(d2.getYear()-d1.getYear());
return rtn;
}
public static Date dateAdd(Date d,int x,int con)
{if (con==0) d.setDate(d.getDate()+x);
if (con==1) d.setMonth(d.getMonth()+x);
if (con==2) d.setYear(d.getYear()+x);
return d;
}
public static int getWeekOfDay(String str)
{
String[] arr=str.split("-");
Date d=new Date(Integer.parseInt(arr[0])-1900,Integer.parseInt(arr[1])-1,Integer.parseInt(arr[2]));
int temp=d.getDay();
if (temp==0) temp=7;
return temp;
}
public static int getDayOfMonth(int year,int month)
{
if (month==1||month==3||month==5||month==7||month==8||month==10||month==12) return 31;
if (month==4||month==6||month==9||month==11) return 30;
if (month==2)
{
if (year%400==0) return 29;
if (year%100!=0&&year%4==0) return 29;
}
return 28;
}
public static void main(String[] args)
{
}
}
java没有vb中的dateDiff函数什么的,只能自己做库函数
上面是我在实际开发中写的工具库,public static int dateDiff(Date d1,Date d2,int f)
这个函数使用来算日期相减的,可以看到int f使用来传入时间单位的(0-日,1-月,2-年)
xuender 2005-11-03
  • 打赏
  • 举报
回复
用2楼的做法,甚至可以农历加减
ifirefox 2005-11-03
  • 打赏
  • 举报
回复
楼上这位,
final long DAY = 3600*1000*24;
这个没考虑到闰年吧
believefym 2005-11-03
  • 打赏
  • 举报
回复
final long DAY = 3600*1000*24;
DateFormat f = new SimpleDateFormat("yyyy-MM-dd");
Date d1 = f.parse(f.format(new Date()));
Date d2 = f.parse("2005-11-1");

System.out.println((d1.getTime()-d2.getTime())/DAY);
liu_you 2005-11-03
  • 打赏
  • 举报
回复
Calendar cal=Calendar.getInstance();
Date d1,d2;
cal.setTime(d1);
cal.add(Calendar.YEAR,-6);//表示此时的六年前
if(d2.before(cal.getTime()))
{
d2比d1六年前还要早;
}
else
{
}
//同样的三个月

62,629

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧