Date[] d=new Date[2];
d[0]=new Date("");
d[1]=new Date("");
Calendar[] cal=new Calendar[2];
for(int i=0;i<callength;i++){
cal[i]=CalendargetInstance();
cal[i]setTime(d[i]);
cal[i]set(CalendarHOUR_OF_DAY,0);
cal[i]set(CalendarMINUTE,0);
cal[i]set(CalendarSECOND,0);
}
long m=cal[0]getTime()getTime();
long n=cal[1]getTime()getTime();
int k=(int)Mathabs((m-n)/1000/3600/24);
方法二:
public static int getDiffDays(String beginDate, String endDate)throws DefaultException{
javautilDate td1 = null;
try {
td1 = new SimpleDateFormat("yyyy-MM-dd")parse(beginDate);
} catch (ParseException e1) {
e1printStackTrace();
}
long lBeginTime = td1getTime();
javautilDate td2 = null;
try {
td2 = new SimpleDateFormat("yyyy-MM-dd")parse(endDate);
Systemoutprintln(td2);
} catch (ParseException e) {
eprintStackTrace();
}
long lEndTime = td2getTime();
int iDay = (int) ((lEndTime - lBeginTime) / 86400000);
return iDay;
方法三
import javatextParseException;
import javatextSimpleDateFormat;
import javautilDate;
class Test2 {
public static String getDate(){
SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd");
Date dd = new Date();
return ftformat(dd);
}
public static long getQuot(String time1, String time2){
long quot = 0;
SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd");
try {
Date date1 = ftparse( time1 );
Date date2 = ftparse( time2 );
quot = date1getTime() - date2getTime();
quot = quot / 1000 / 60 / 60 / 24;
} catch (ParseException e) {
eprintStackTrace();
}
return quot;
}
public static void main(String[] args) throws Exception {
String date1 = "2008-8-8";
String date2 = getDate();
long day = getQuot(date1,date2);
Systemoutprintln( "距离"+date1+" 还有 "+day+" 天" );
}
}
//javatextSimpleDateFormat( "yyyy/MM/dd":String )
//++++ parse(String):Date ->ParseException
//++++ format(Date):String String格式为new SimpleDateFormat(String)参数格式
//javautilDate
//++++ getTime():long 返回1970年1月1日之前之后的时刻(毫秒)
利用javautilGregorianCalendar计算日期差(毫秒表示)
import javautil;
public class ElapsedMillis {
public static void main(String[] args) {
GregorianCalendar gc1 = new GregorianCalendar(1995, 11, 1, 3, 2, 1);
GregorianCalendar gc2 = new GregorianCalendar(1995, 11, 1, 3, 2, 2);
// the above two dates are one second apart
Date d1 = gc1getTime();
Date d2 = gc2getTime();
long l1 = d1getTime();
long l2 = d2getTime();
long difference = l2 - l1;
Systemoutprintln("Elapsed milliseconds: " + difference);
}
}import javaioBufferedInputStream;
import javaioDataInputStream;
import javaioIOException;
import javatextSimpleDateFormat;
import javautilDate;
public class Cntdate {
/
@param args
@throws Exception
/
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
String datestr1,datestr2;
long d1,d2,d3;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Systemoutprintln("输入第一个日期(yyyy-mm-dd):");
datestr1 = new DataInputStream(new BufferedInputStream(Systemin))readLine();
Systemoutprintln("输入第二个日期(yyyy-mm-dd):");
datestr2 = new DataInputStream(new BufferedInputStream(Systemin))readLine();
d1 = sdfparse(datestr1)getTime();
d2 = sdfparse(datestr2)getTime();
d3 = Mathabs((d1-d2)/1000/60/60/24);
Systemoutprintln(d3);
}
}import javautil;
public class MyDate{
int day;
int month;
int year;
public MyDate(){}
public MyDate(int day,int month,int year){
thisday = day;
thismonth = month;
thisyear = year;
}
public void funtion(MyDate d){
int newDay = dday;
int newMonth = dmonth;
int newYear = dyear;
Calendar c1 = CalendargetInstance();
c1set(newYear,newMonth,newDay);
long n1 = c1getTimeInMillis();
Calendar c2 = CalendargetInstance();
c2set(year,month,day);
long n2 = c2getTimeInMillis();
Systemoutprintln( "相差天数为:" + Mathabs((n1 - n2)/24/3600000)); }
public static void main(String[] args){
MyDate d1 = new MyDate(1,1,2008);
MyDate d2 = new MyDate(2,1,2001);
d1funtion(d2);
}
}看完了jdk的help,发现sun没有提供这样的一个函数,朋友给了几个实现方法//取得剩余天数SimpleDateFormat df=new SimpleDateFormat("yyyymmdd");Date d0=new javautilDate();Date d1=dfparse(end_date);long time0=d0getTime();long time1=d1getTime();Systemoutprintln((time1-time0)/(1000606024)); 这样算两个时间相差的天数比较好 / 计算两个日期之间相差的天数 @param date1 @param date2 @return/public static int diffdates(Date date1, Date date2) {int result = 0;ElapsedTime et = new ElapsedTime(); GregorianCalendar gc1 = new GregorianCalendar();GregorianCalendar gc2 = new GregorianCalendar(); gc1setTime(date1);gc2setTime(date2);result = etgetDays(gc1, gc2); return result;}然后ElapseTime中的方法是:public int getDays(GregorianCalendar g1, GregorianCalendar g2) {int elapsed = 0;GregorianCalendar gc1, gc2; if (g2after(g1)) {gc2 = (GregorianCalendar) g2clone();gc1 = (GregorianCalendar) g1clone();} else {gc2 = (GregorianCalendar) g1clone();gc1 = (GregorianCalendar) g2clone();} gc1clear(CalendarMILLISECOND);gc1clear(CalendarSECOND);gc1clear(CalendarMINUTE);gc1clear(CalendarHOUR_OF_DAY); gc2clear(CalendarMILLISECOND);gc2clear(CalendarSECOND);gc2clear(CalendarMINUTE);gc2clear(CalendarHOUR_OF_DAY); while (gc1before(gc2)) {gc1add(CalendarDATE, 1);elapsed++;}return elapsed;}其实使用joda最简单public boolean isRentalOverdue(DateTime datetimeRented) {
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)