java代码取天数 java获取后天日期( 三 )


try{
return (sdf.parse(strDate));
}catch (Exception e){
return null;
}
}
例2
代码如下复制代码
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class test16 {
/**
* @param args
* @throws ParseException
*/
public static void main(String[] args) throws ParseException {
// TODO Auto-generated method stub
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date d1=sdf.parse("2012-09-08 10:10:10");
Date d2=sdf.parse("2012-09-15 00:00:00");
System.out.println(daysBetween(d1,d2));
System.out.println(daysBetween("2012-09-08 10:10:10","2012-09-15 00:00:00"));
}
/**
* 计算两个日期之间相差的天数
* @param smdate 较小的时间
* @param bdate较大的时间
* @return 相差天数
* @throws ParseException
*/
public static int daysBetween(Date smdate,Date bdate) throws ParseException
{
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
smdate=sdf.parse(sdf.format(smdate));
bdate=sdf.parse(sdf.format(bdate));
Calendar cal = Calendar.getInstance();
cal.setTime(smdate);
long time1 = cal.getTimeInMillis();
cal.setTime(bdate);
long time2 = cal.getTimeInMillis();
long between_days=(time2-time1)/(1000*3600*24);
return Integer.parseInt(String.valueOf(between_days));
}
/**
*字符串的日期格式的计算
*/
public static int daysBetween(String smdate,String bdate) throws ParseException{
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
Calendar cal = Calendar.getInstance();
cal.setTime(sdf.parse(smdate));
long time1 = cal.getTimeInMillis();
cal.setTime(sdf.parse(bdate));
long time2 = cal.getTimeInMillis();
long between_days=(time2-time1)/(1000*3600*24);
return Integer.parseInt(String.valueOf(between_days));
}
}
例3
代码如下复制代码
//取得剩余天数
SimpleDateFormat df=new SimpleDateFormat("yyyymmdd");
Date d0=new java.util.Date();
Date d1=df.parse(end_date);
long time0=d0.getTime();
long time1=d1.getTime();
System.out.println((time1-time0)/(1000*60*60*24));
这样算两个时间相差的天数比较好
代码如下复制代码
/**
* 计算两个日期之间相差的天数
*
* @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();
gc1.setTime(date1);
gc2.setTime(date2);
result = et.getDays(gc1, gc2);
return result;
}
然后ElapseTime中的方法是:
代码如下复制代码
public int getDays(GregorianCalendar g1, GregorianCalendar g2) {
int elapsed = 0;
GregorianCalendar gc1, gc2;
if (g2.after(g1)) {
gc2 = (GregorianCalendar) g2.clone();
gc1 = (GregorianCalendar) g1.clone();
} else {
gc2 = (GregorianCalendar) g1.clone();
gc1 = (GregorianCalendar) g2.clone();
}
gc1.clear(Calendar.MILLISECOND);
gc1.clear(Calendar.SECOND);
gc1.clear(Calendar.MINUTE);
gc1.clear(Calendar.HOUR_OF_DAY);
gc2.clear(Calendar.MILLISECOND);
gc2.clear(Calendar.SECOND);
gc2.clear(Calendar.MINUTE);
gc2.clear(Calendar.HOUR_OF_DAY);
while (gc1.before(gc2)) {
gc1.add(Calendar.DATE, 1);
elapsed++;
}
return elapsed;
}
其实使用joda最简单
代码如下复制代码
public boolean isRentalOverdue(DateTime datetimeRented) {
Period rentalPeriod = Period.days(2);

推荐阅读