Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate Date ranges in JodaTime

Tags:

java

jodatime

I need to check if a date(in string) exists in array list.

I have two dates, first i need to generate date ranges between these two dates and store them in an Array. This is what I am doing.

DateTimeFormatter dateFromatter= DateTimeFormat.forPattern("MM/dd/yyyy");

DateTime startDate= formatter.parseDateTime("01/02/2012");
DateTime endDate= formatter.parseDateTime("01/31/2012");

 List<LocalDate> dates = new ArrayList<LocalDate>();


  int days = Days.daysBetween(startDate, endDate).getDays();
  for (int i=0; i < days; i++) {

This is where i am getting problem.

Type mismatch: cannot convert from DateTime to LocalDate

>   LocalDate listOfDates =
> startDate.withFieldAdded(DurationFieldType.days(), i);
> dates.add(listOfDates);

  }
like image 224
Namita Avatar asked Feb 16 '12 14:02

Namita


1 Answers

Use org.joda.time.Interval

Interval interval = new Interval(startDate, endDate);  
for (LocalDate date : dates)  
{   
  if (interval.contains(date))  
   // 
like image 91
Ilya Avatar answered Sep 28 '22 01:09

Ilya