如何获得一年的开始和结束日期? [英] How to get start and end date of a year?
本文介绍了如何获得一年的开始和结束日期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我必须使用Java Date
类来解决这个问题(它与我控制的东西接触)。
I have to use the Java Date
class for this problem (it interfaces with something out of my control).
如何获得一年的开始和结束日期,然后遍历每个日期?
How do I get the start and end date of a year and then iterate through each date?
推荐答案
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, 2014);
cal.set(Calendar.DAY_OF_YEAR, 1);
Date start = cal.getTime();
//set date to last day of 2014
cal.set(Calendar.YEAR, 2014);
cal.set(Calendar.MONTH, 11); // 11 = december
cal.set(Calendar.DAY_OF_MONTH, 31); // new years eve
Date end = cal.getTime();
//Iterate through the two dates
GregorianCalendar gcal = new GregorianCalendar();
gcal.setTime(start);
while (gcal.getTime().before(end)) {
gcal.add(Calendar.DAY_OF_YEAR, 1);
//Do Something ...
}
这篇关于如何获得一年的开始和结束日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文