1

I have two dates which the user should select: "From Date" and "To Date". The form should be updating such that the range of dates which the user can select for the "To Date" should consist of the week containing the "From Date".

For example, if the "From Date" is 23rd May then the "To Date" should be selected from 19, 20, 21, 22, 23, 24 or 25 of May and selecting dates out of that week should be impossible.

I am not able find the possible solution for this. Please help me to solve this.

Here is my code:

SimpleDateFormat sdf = new SimpleDateFormat("MM-dd-yyyy");
LocalDate now = new LocalDate(fromDate);
LocalDate monday = now.withDayOfWeek(DateTimeConstants.MONDAY);
System.out.printn(monday.toDate());

Using this code, I can get all the dates of that week and compare them, but I don't want to do it like this.

user2582360
  • 55
  • 3
  • 13

1 Answers1

1

the "To Date" should consist of the week containing the "From Date".

How to check that both dates fall in the same week?

  • get the week of the year and year of from date and to date
  • compare week and year of both the date

Sample code:

int week1 = fromDate.get(DateTimeFieldType.weekOfWeekyear());
int year1 = fromDate.get(DateTimeFieldType.year());

int week2 = toDate.get(DateTimeFieldType.weekOfWeekyear());
int year2 = toDate.get(DateTimeFieldType.year());

if(year1 == year2 && week1 == week2){
      System.out.print("In the range of same week");
}
Community
  • 1
  • 1
Braj
  • 46,415
  • 5
  • 60
  • 76