38731

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

<div class="question-status question-originals-of-duplicate">

This question already has an answer here:

    <li> Get all days between tomorrow and 60 days forward then loop through them <span class="question-originals-answer-count"> 3 answers </span> </li> </ul>

    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 }

    Answer1:

    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
    
    </pre>
        

    Answer2:

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

    Answer3:

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

    Answer4:

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

Recommend

  • Calendar return wrong current date android
  • How can I setMaxDate() for DatePicker to one month after the current date?
  • DialogFragment behaving unexpectedly
  • How to finish activity when date picker dialog on cancel button is clicked
  • New Firebase failed: First argument must be a valid firebase URL and the path can't contain “.”
  • JSON encode and decode on PHP
  • Building Qt project for C++11 standard
  • presentShareDialogWithParams posts to FB wall, but callback handler results say error
  • Display java JPanel in a JFrame
  • Eloquent update method change created_at timestamp
  • How to Cache Real-time Data?
  • MVC3 Razor - ListBox pre-select not working
  • why overloaded new operator is calling constructor even I am using malloc inside overloading functio
  • how to adjust image in a panel in Java swing?
  • How to rebase a series of branches?
  • Why value captured by reference in lambda is broken? [duplicate]
  • Spring Data JPA custom method causing PropertyReferenceException
  • Illegal mix of collations for operation for date/time comparison
  • req.body is undefined - nodejs
  • Possible to stop flickering java tooltip in heavyweight mode?
  • output of program is not same as passed argument
  • sending/ receiving email in Java
  • Akka Routing: Reply's send to router ends up as dead letters
  • AT Commands to Send SMS not working in Windows 8.1
  • Cannot Parse HTML Data Using Android / JSOUP
  • using conditional logic : check if record exists; if it does, update it, if not, create it
  • How to delete a row from a dynamic generate table using jquery?
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • Proper way to use connect-multiparty with express.js?
  • How to set the response of a form post action to a iframe source?
  • JTable with a ScrollPane misbehaving
  • Change div Background jquery
  • Qt: Run a script BEFORE make
  • apache spark aggregate function using min value
  • unknown Exception android
  • Checking variable from a different class in C#
  • Sorting a 2D array using the second column C++
  • reshape alternating columns in less time and using less memory
  • failed to connect to specific WiFi in android programmatically
  • How can I use threading to 'tick' a timer to be accessed by other threads?