在Java之中用Calendar方法,我们可以容易的实现日期相关的计算:
public class TestDate {
public static void main(String[] args) {
DateFormat format = new SimpleDateFormat(“yyyy-MM-dd”);
Date date1 = null, date2 = null;
try {
date1 = format.parse(“2010-10-01”);
date2 = format.parse(“2011-10-02”);
} catch (ParseException e) {
e.printStackTrace();
}
long diff = date1.getTime() - date2.getTime();//计算两个日期之前相差的毫秒数
long days = diff / (24 * 60 * 60 * 1000)http://www.cqywzx.com/;//除以1天的毫秒数,得到相差的天数
System.out.println(“这两个日期之间的天数为: ” + days);
}
}
讲解对于Java中如何计算日期之间的天数知识
原创
©著作权归作者所有:来自51CTO博客作者jgsa的原创作品,谢绝转载,否则将追究法律责任
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章