Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Date Util in java that will explode date range in weeks,month,quarter,year

Tags:

java

date

I am looking for a java library that when given from and to date would return a list of dates in weeks,months,quarter or year which ever is most applicable. I have done this manually and i wanted to know if this is already implemented and tested as a part of a standard package.

Example

Given 1/1/2009 , 1/4/2009 it should give 1/1/2009,1/2/2009,1/3/2009,1/4/2009

Given 1/1/2009 , 1/14/2009 it should give 1/1/2009,1/7/2009,1/14/2009

hope you that is clear :)

like image 465
Bharani Avatar asked Dec 17 '22 03:12

Bharani


2 Answers

The DateTime class provided by Joda Time has methods such as plusDays(int), plusWeeks(int), plusMonths(int) which should help.

Assuming you want to get all the dates between start and end in weeks (pseudocode):

DateTime start = // whatever
DateTime end = // whatever

List<DateTime> datesBetween = new ArrayList<DateTime>();

while (start <= end) {
   datesBetween.add(start);
   DateTime dateBetween = start.plusWeeks(1);
   start = dateBetween;
}
like image 125
Dónal Avatar answered Dec 19 '22 16:12

Dónal


Alternative to Jado is use standart java API

Calendar start = // whatever
Calendar end = // whatever

List<Calendar> datesBetween = new ArrayList<Calendar>();

while (start.compareTo(end) <= 0) {
   datesBetween.add(start);
   Calendar dateBetween = start.add(Calendar.DAY_OF_MONTH, 7);
   start = dateBetween;
}
like image 45
Joter Avatar answered Dec 19 '22 18:12

Joter