我需要明天再加上60天,然后每天循环播放。只是想知道这样做的恰当方法是什么?
这就是我试过的
Calendar startCalemder = Calendar.getInstance();
startCalemder.setTime(new Date());
startCalemder.add(Calendar.DATE, 1);
Calendar endCalendar = Calendar.getInstance();
endCalendar.setTime(new Date());
endCalendar.add(Calendar.DATE, 60);
//loop over day by day
for (; startCalemder.compareTo(endCalendar) <= 0;
startCalemder.add(Calendar.DATE, 1)) {
startCalemder.get(Calendar.YEAR); //shows year
startCalemder.get(Calendar.MONTH); //shows month
startCalemder.get(Calendar.DAY_OF_MONTH); //shows day
}
答案 0 :(得分:0)
我认为在这种情况下while循环更具可读性,另外我会使用endCalendar.after(startCalemder)
方法而不是startCalemder.compareTo(endCalendar) <= 0
来检查当前日期是否小于结束日期
while ( endCalendar.after(startCalemder) ) {
startCalemder.get(Calendar.YEAR); //shows year
startCalemder.get(Calendar.MONTH); //shows month
startCalemder.get(Calendar.DAY_OF_MONTH); //shows day
startCalemder.add(Calendar.DATE, 1);
}
答案 1 :(得分:0)
如果您想尝试使用Java 8中的新日期类,可以执行以下操作:
LocalDate tomorrow = LocalDate.now().plusDays(1);
final LocalDate end = tomorrow.plusDays(60);
while (tomorrow.isBefore(end)) {
System.out.println(tomorrow);
tomorrow = tomorrow.plusDays(1);
}
答案 2 :(得分:0)
你可以这样做:
public static void main(String[] args) {
final Calendar c = Calendar.getInstance();
//set the timestamp info to 00:00:00 so that we can compare the dates later if needed
c.set(Calendar.HOUR_OF_DAY, 0);
c.set(Calendar.MINUTE, 0);
c.set(Calendar.SECOND, 0);
c.set(Calendar.MILLISECOND, 0);
final int days = 61; //61 because we will add the result after we add the day
final List<Date> datesList= new ArrayList<Date>(); //list to store each date object
for (int i = 0; i < days; i++) {
c.add(Calendar.DATE, 1); //add one day to the calendar (so first value is tomorrow)
datesList.add(new Date(c.getTimeInMillis())); //store each day in the list
}
//iterate through the list and do whatever you want with the dates
for (Date date : datesList) {
System.out.println(date);
}
}
输出:
Thu Nov 20 00:00:00 EET 2014 Fri Nov 21 00:00:00 EET 2014 Sat Nov 22 00:00:00 EET 2014 Sun Nov 23 00:00:00 EET 2014 Mon Nov 24 00:00:00 EET 2014 Tue Nov 25 00:00:00 EET 2014 Wed Nov 26 00:00:00 EET 2014 Thu Nov 27 00:00:00 EET 2014 Fri Nov 28 00:00:00 EET 2014 Sat Nov 29 00:00:00 EET 2014 Sun Nov 30 00:00:00 EET 2014 Mon Dec 01 00:00:00 EET 2014 Tue Dec 02 00:00:00 EET 2014 Wed Dec 03 00:00:00 EET 2014 Thu Dec 04 00:00:00 EET 2014 Fri Dec 05 00:00:00 EET 2014 Sat Dec 06 00:00:00 EET 2014 Sun Dec 07 00:00:00 EET 2014 Mon Dec 08 00:00:00 EET 2014 Tue Dec 09 00:00:00 EET 2014 Wed Dec 10 00:00:00 EET 2014 Thu Dec 11 00:00:00 EET 2014 Fri Dec 12 00:00:00 EET 2014 Sat Dec 13 00:00:00 EET 2014 Sun Dec 14 00:00:00 EET 2014 Mon Dec 15 00:00:00 EET 2014 Tue Dec 16 00:00:00 EET 2014 Wed Dec 17 00:00:00 EET 2014 Thu Dec 18 00:00:00 EET 2014 Fri Dec 19 00:00:00 EET 2014 Sat Dec 20 00:00:00 EET 2014 Sun Dec 21 00:00:00 EET 2014 Mon Dec 22 00:00:00 EET 2014 Tue Dec 23 00:00:00 EET 2014 Wed Dec 24 00:00:00 EET 2014 Thu Dec 25 00:00:00 EET 2014 Fri Dec 26 00:00:00 EET 2014 Sat Dec 27 00:00:00 EET 2014 Sun Dec 28 00:00:00 EET 2014 Mon Dec 29 00:00:00 EET 2014 Tue Dec 30 00:00:00 EET 2014 Wed Dec 31 00:00:00 EET 2014 Thu Jan 01 00:00:00 EET 2015 Fri Jan 02 00:00:00 EET 2015 Sat Jan 03 00:00:00 EET 2015 Sun Jan 04 00:00:00 EET 2015 Mon Jan 05 00:00:00 EET 2015 Tue Jan 06 00:00:00 EET 2015 Wed Jan 07 00:00:00 EET 2015 Thu Jan 08 00:00:00 EET 2015 Fri Jan 09 00:00:00 EET 2015 Sat Jan 10 00:00:00 EET 2015 Sun Jan 11 00:00:00 EET 2015 Mon Jan 12 00:00:00 EET 2015 Tue Jan 13 00:00:00 EET 2015 Wed Jan 14 00:00:00 EET 2015 Thu Jan 15 00:00:00 EET 2015 Fri Jan 16 00:00:00 EET 2015 Sat Jan 17 00:00:00 EET 2015 Sun Jan 18 00:00:00 EET 2015 Mon Jan 19 00:00:00 EET 2015
答案 3 :(得分:-1)
你可以试试这个
public static void main(String[] args){
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
Date now = new Date();
long dayInMillis = 1000*60*60*24;
long nowInMillis = System.currentTimeMillis();
for(long tmp = dayInMillis;tmp<=dayInMillis*60;tmp +=dayInMillis){
now.setTime(nowInMillis+tmp);
System.out.println(" date: "+sdf.format(now));
}
}