Get all days from tomorrow and two months forward and loop through them [duplicate]

Deadly 提交于 2020-01-24 14:23:49

问题


I need to take tomorrow, add 60 days to it and loop over it day by day. Just wondering what would be the appropriate way of doing this?

This is what I tried

    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
    }

回答1:


You could do it like this:

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);
    }
}

Ouput:

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




回答2:


I think that in this case while loop is a bit more readable, additionally I would use endCalendar.after(startCalemder) method istead of startCalemder.compareTo(endCalendar) <= 0 to check if current date is less than end date

      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);
        }



回答3:


If you want to try with the new date classes in Java 8 you can do:

    LocalDate tomorrow = LocalDate.now().plusDays(1);
    final LocalDate end = tomorrow.plusDays(60);
    while (tomorrow.isBefore(end)) {
        System.out.println(tomorrow);
        tomorrow = tomorrow.plusDays(1);
    }



回答4:


You can try this

 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));
     }
 }


来源:https://stackoverflow.com/questions/27019788/get-all-days-from-tomorrow-and-two-months-forward-and-loop-through-them

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!