在Java开发物流或是其他功能的时候会用到两个日期相差多天的数据,所以整理了一下备用。
调用方式:
代码如下 复制代码
long date1 = getDateTime("20121201");//可改成自己的日期类型,但以“20121212”这种格式
long date2 = getDateTime("20121212");
int day = dateInterval(date1, date2);
System.out.println(day);
具体实现方法调用:
代码如下 复制代码
public static int dateInterval(long date1, long date2) {
if(date2 > date1){
date2 = date2 + date1;
date1 = date2 - date1;
date2 = date2 - date1;
}
// Canlendar 该类是一个抽象类
// 提供了丰富的日历字段
// 本程序中使用到了
// Calendar.YEAR 日期中的年份
// Calendar.DAY_OF_YEAR 当前年中的天数
// getActualMaximum(Calendar.DAY_OF_YEAR) 返回今年是 365 天还是366天
Calendar calendar1 = Calendar.getInstance(); // 获得一个日历
calendar1.setTimeInMillis(date1); // 用给定的 long 值设置此 Calendar 的当前时间值。
Calendar calendar2 = Calendar.getInstance();
calendar2.setTimeInMillis(date2);
// 先判断是否同年
int y1 = calendar1.get(Calendar.YEAR);
int y2 = calendar2.get(Calendar.YEAR);
int d1 = calendar1.get(Calendar.DAY_OF_YEAR);
int d2 = calendar2.get(Calendar.DAY_OF_YEAR);
int maxDays = 0;
int day = 0;
if(y1 - y2 > 0){
day = numerical(maxDays, d1, d2, y1, y2, calendar2);
}else{
day = d1 - d2;
}
return day;
}
public static int numerical(int maxDays, int d1, int d2, int y1, int y2, Calendar calendar){
int day = d1 - d2;
int betweenYears = y1 - y2;
List d366 = new ArrayList();
if(calendar.getActualMaximum(Calendar.DAY_OF_YEAR) == 366){
System.out.println(calendar.getActualMaximum(Calendar.DAY_OF_YEAR));
day += 1;
}
for (int i = 0; i < betweenYears; i++) {
// 当年 + 1 设置下一年中有多少天
calendar.set(Calendar.YEAR, (calendar.get(Calendar.YEAR)) + 1);
maxDays = calendar.getActualMaximum(Calendar.DAY_OF_YEAR);
// 第一个 366 天不用 + 1 将所有366记录,先不进行加入然后再少加一个
if(maxDays != 366){
day += maxDays;
}else{
d366.add(maxDays);
}
// 如果最后一个 maxDays 等于366 day - 1
if(i == betweenYears-1 && betweenYears > 1 && maxDays == 366){
day -= 1;
}
}
for(int i = 0; i < d366.size(); i++){
// 一个或一个以上的366天
if(d366.size() >= 1){
day += d366.get(i);
}
}
return day;
}
public static long getDateTime(String strDate) {
return getDateByFormat(strDate, "yyyyMMdd").getTime();
}
public static Date getDateByFormat(String strDate, String format) {
SimpleDateFormat sdf = new SimpleDateFormat(format);
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 {
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"));
}
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));
这样算两个时间相差的天数比较好
代码如下 复制代码
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);
return datetimeRented.plus(rentalPeriod).isBeforeNow()
}