SoFunction
Updated on 2025-03-08

Java traversal of all date operations in the middle of the start and end date

The incoming start and end date returns a collection with generic String:

private static List<String> dateUtil(Date start, Date end) {
    List<String> list = new ArrayList<String>();
    long s = ();
    long e = ();

    Long oneDay = 1000 * 60 * 60 * 24l;

    while (s <= e) {
      start = new Date(s);
      (new SimpleDateFormat("yyyy-MM-dd").format(start));
      s += oneDay;
    }
    return list;
  }

Supplementary knowledge:Loop of dates in java

I have read a lot of date loop codes, but I don’t need to read those that don’t use calendar. There are various loop judgments, only this one is more reliable.

import ;
import ;
import ;
import ; 
 
public class Test{
	
	@Test
	public void forDate() {
		// Date formatting		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		try {
			// Start date			Date d1 = ("2018-2-25");
			// End date			Date d2 = ("2018-3-5");
			Date tmp = d1;
			Calendar dd = ();
			(d1);
			// Print dates from February 25, 2018 to March 5, 2018			while (() &lt; ()) {
				tmp = ();
				((tmp));
				// Add 1 to the number of days				(Calendar.DAY_OF_MONTH, 1);
			}
		} catch (ParseException e) {
			();
		}
	}
 
}

All the date operations in the above Java traversal start and end date are all the contents I share with you. I hope you can give you a reference and I hope you can support me more.