I have a java program which will check for start date and end date of each item. Each Item must have their own specific start date and end date range. And this system will promp
Using Guava Range may simplify your question
public class ValidateDateFall {
public static void main(String args[]) throws ParseException {
List- itemArr = Lists.newArrayList();
itemArr.add(new Item("11A", "01/08/2014", "01/09/2014"));
itemArr.add(new Item("11B", "02/09/2014", "30/09/2014"));
itemArr.add(new Item("11C", "18/08/2014", "30/08/2014"));
Range
currentPeriod = null;
for(Item item:itemArr) {
// Initial Current Range
if(currentPeriod == null) {
currentPeriod = Range.closed(item.startDate, item.endDate);
continue;
}
// Check item start date and end date is within in current period
Range itemRange = Range.closed(item.startDate, item.endDate);
boolean isEnclosed = currentPeriod.encloses(itemRange);
// if within current item then prompt error message or not then extend the period
if(isEnclosed) {
System.out.println("Prompt Error Message:" + item);
} else {
currentPeriod = currentPeriod.span(itemRange);
}
}
}
}
// Item Pojo
class Item{
private static final SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
public String name;
public Date startDate;
public Date endDate;
public Item(String name, String startDateStr, String endDate) throws ParseException {
this.name = name;
this.startDate = sdf.parse(startDateStr);
this.endDate = sdf.parse(endDate);;
}
@Override
public String toString() {
return Objects.toStringHelper(this.getClass()).add("start date", startDate).add("end date", endDate).toString();
}
}