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");
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
不要误会我说的2楼是liu_you(滴水藏海)
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-年)
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);
Date d1,d2;
cal.setTime(d1);
cal.add(Calendar.YEAR,-6);//表示此时的六年前
if(d2.before(cal.getTime()))
{
d2比d1六年前还要早;
}
else
{
}
//同样的三个月
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;
这个没考虑到闰年吧
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
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-年)
getDayOfMonth(int year,int month)
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+"分钟");
}
}
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;
}
xuender(徐强)大哥,你的方法我有去试,但不行啊。不知道是不是我写的SQL语句有错。
http://community.csdn.net/Expert/topic/4368/4368711.xml?temp=.9411127
http://community.csdn.net/Expert/topic/4368/4368711.xml?temp=.9411127
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);