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
You can do something like this:
public class DateDemo {
public static void main(String[] args) {
Date date = new Date(11, 5, 21);
Date date2 = new Date(15, 1, 21);
// tests if date2 is before date and prints result
boolean before = date2.before(date);
if (before) {
System.out.println("Date 2 is before date: " + before);
throw new RuntimeException("Error with dates");
}
}
}
Of course, it is just an example, since I don't know where this date is coming from. And you will have to do it inside a loop to verify all of them.
Date ranges can be tricky...
First you want to make sure that the original start date is not equal to the compare start or end date and that the original end date is not equal to the compare start or end date
startDate.equals(originalStartDate) ||
startDate.equals(originalEndDate) ||
endDate.equals(originalStartDate) ||
endDate.equals(originalEndDate)
If that's all okay (false
), you then need to check if the compare start date falls between the original start or end date and if the compare end date falls between the original start or end date...
(startDate.after(originalStartDate) || startDate.before(originalEndDate) ||
(endDate.after(originalStartDate) || endDate.before(originalEndDate)
This tries to capture any point where the two ranges either include or overlap each other...
And because I actual wrote some test code...
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
public class CompareDates {
public static void main(String[] args) {
try {
List<Event> events = new ArrayList<Event>(3);
events.add(new Event(toDate("01/08/2014"), toDate("01/09/2014")));
events.add(new Event(toDate("02/09/2014"), toDate("30/09/2014")));
Date start = toDate("18/08/2014");
Date end = toDate("30/08/2014");
for (Event evt : events) {
System.out.println(evt.conflicts(start, end));
}
} catch (ParseException exp) {
exp.printStackTrace();
}
}
public static Date toDate(String value) throws ParseException {
return new SimpleDateFormat("dd/MM/yyyy").parse(value);
}
public static class Event {
private Date startDate;
private Date endDate;
public Event(Date startDate, Date endDate) {
this.startDate = startDate;
this.endDate = endDate;
}
public boolean conflicts(Date start, Date end) {
return start.equals(startDate) ||
start.equals(endDate) ||
end.equals(startDate) ||
end.equals(endDate) ||
(start.after(startDate) && start.before(endDate)) ||
(end.after(startDate) && end.before(endDate));
}
}
}
Using Guava Range may simplify your question
public class ValidateDateFall {
public static void main(String args[]) throws ParseException {
List<Item> 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<Date> 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<Date> 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();
}
}
Here is simple unit test of a possible implementation using Joda-Time and Guava's Range.
import com.google.common.collect.Lists;
import com.google.common.collect.Range;
import org.joda.time.LocalDate;
import org.junit.Test;
import java.util.List;
import static org.junit.Assert.assertEquals;
public class ItemDateRangeTest {
static class Item {
private final String id;
private final LocalDate startDate;
private final LocalDate endDate;
public Item(String id, LocalDate startDate, LocalDate endDate) {
this.id = id;
this.startDate = startDate;
this.endDate = endDate;
}
public String getId() {
return id;
}
public LocalDate getStartDate() {
return startDate;
}
public LocalDate getEndDate() {
return endDate;
}
public Range<LocalDate> getRange() {
// Closed range includes the lower and upper bounds
return Range.closed(startDate, endDate);
}
}
static class ItemStore {
private final List<Item> items = Lists.newArrayList();
public void add(Item newItem) throws IllegalArgumentException{
for (Item item : items) {
if (item.getRange().isConnected(newItem.getRange())) {
throw new IllegalArgumentException("error: date range overlap!");
}
}
items.add(newItem);
}
public List<Item> getItems() {
return items;
}
}
@Test()
public void testItemDateRangeNoOverlap() {
final ItemStore itemStore = new ItemStore();
Item itemA = new Item("11A", new LocalDate(2014, 8, 1), new LocalDate(2014, 9, 1));
Item itemB = new Item("11B", new LocalDate(2014, 9, 2), new LocalDate(2014, 9, 30));
itemStore.add(itemA);
itemStore.add(itemB);
assertEquals(itemStore.getItems().get(0), itemA);
assertEquals(itemStore.getItems().get(1), itemB);
}
@Test(expected = IllegalArgumentException.class)
public void testItemDateRangeWithOverlap() {
final ItemStore itemStore = new ItemStore();
itemStore.add(new Item("11A", new LocalDate(2014,8,1), new LocalDate(2014,9,1)));
itemStore.add(new Item("11B", new LocalDate(2014,9,2), new LocalDate(2014,9,30)));
itemStore.add(new Item("11C", new LocalDate(2014,8,18), new LocalDate(2014,8,30)));
}
}