Java 比较日期相差多少天

//1 创建日期格式

Date old = new Date();

Date news = new Date();

//2 设置日期格式
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
old=sdf.parse("2016-06-25 12:22:55");
news=sdf.parse(sdf.format(news));

//创建日历类, 获取毫秒 在转换为 天数
Calendar cal = Calendar.getInstance();
cal.setTime(old);
long time1 = cal.getTimeInMillis();
cal.setTime(news);
long time2 = cal.getTimeInMillis();
long between_days=(time2-time1)/(1000*3600*24);

System.out.println(between_days);  

cal.get(Calendar.YEAR)

cal.get(Calendar.DAY) //获取天

cal.get(Calendar.DAY_OF_WEEk) //一周类的第几天

 

posted @ 2016-06-29 09:09  大善若龙  阅读(1265)  评论(0编辑  收藏  举报