1、Java日期循环
SimpleDateFormat formater = new SimpleDateFormat("yyyy-MM-dd");
String dateStr1="2008-3-16";
String dateStr2="2008-3-24";
java.util.Date dateBegin=formater.parse(dateStr1);
java.util.Date dateEnd=formater.parse(dateStr2);
Calendar ca=Calendar.getInstance();
while(dateBegin.compareTo(dateEnd)<=0){
ca.setTime(dateBegin);
ca.add(ca.DATE,1);//把dateBegin加上1天然后重新赋值给date1
dateBegin=ca.getTime();
}
2、Java获取当月的第一天和最后一天
Calendar cal = Calendar.getInstance();
SimpleDateFormat datef=new SimpleDateFormat("yyyy-MM-dd");
//当前月的最后一天
cal.set( Calendar.DATE, 1 );
cal.roll(Calendar.DATE, - 1 );
Date endTime=cal.getTime();
String endTime1=datef.format(endTime)+" 23:59:59";
//当前月的第一天
cal.set(GregorianCalendar.DAY_OF_MONTH, 1);
Date beginTime=cal.getTime();
String beginTime1=datef.format(beginTime)+" 00:00:00";
//循环输出日期
public static void main(String[] arg) throws Exception {
GregorianCalendar g = new GregorianCalendar();
g.setTime(new Date());
int day = g.get(Calendar.DAY_OF_YEAR);
int year = g.get(Calendar.YEAR);
for (int i = 0; i < 400; i++) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String formatTime = sdf.format(g.getTime());
System.out.println(formatTime);
if(g.get(Calendar.YEAR) > year) {
day = 0;
year = g.get(Calendar.YEAR);
}
g.set(Calendar.DAY_OF_YEAR, day + 1);
day++;
}
}