Java implements a method for splitting dates

  • 2020-04-01 04:01:55
  • OfStack

This article illustrates how Java implements date splitting. Share with you for your reference. The details are as follows:

For example, calculate the number of days and specific dates between 6-1 and 6-5, and the expected result is:
6-1
6-2
6-3
6-4
6-5

Here's what I did with the Java calendar class:


Calendar canlandar1 = Calendar.getInstance();//The start time
Calendar canlandar2 = Calendar.getInstance();//The end of time
canlandar1.setTime(psd);//2009-6-1
canlandar2.setTime(pfd);//2009-6-5
List<Date> returnList = new ArrayList<Date>();
while(canlandar1.compareTo(canlandar2) < 1){
  returnList.add(canlandar1.getTime());
  canlandar1.add(canlandar1.DATE, 1);//Add one day to each cycle
}

I hope this article has been helpful to your Java programming.


Related articles: