-2

I get three dates: From date, To date and current date.

I want to find whether current date is in between From and To dates. If current date is in between these two dates then I want to create two new From and To dates.

Example:

  • From Date = 15 march
  • To Date = 25 march
  • current date = 21 march

Expected result should be:

  1. From Date= 15 march, To Date=21 march
  2. From date= 21 march, To Date=25 march

To implement this logic I want to check my current date status whether it's in middle of date range or it's before or after.

studx
  • 39
  • 3
y2k
  • 79
  • 1
  • 6
  • These dates, are they Strings or Dates? In case they are already of class Date, you could use the methods before() and after() – Robert Kock Mar 21 '18 at 10:01
  • If you are using java.util.Date. You can also use compareTo. – damjad Mar 21 '18 at 10:02
  • its in Date format – y2k Mar 21 '18 at 10:05
  • 1
    Be careful depending on what a date range represents. Is it OK if `21 March` is a member of both date ranges? Do ranges have to be non-overlapping? You may need to rethink date range so that it is half-open, i.e. the ending date is not part of the range. This is a common issue you must deal with working with ranges of any kind. – Jim Garrison Mar 21 '18 at 10:26
  • 1
    Tip: [`org.threeten.extra.LocalDateRange`](http://www.threeten.org/threeten-extra/apidocs/org/threeten/extra/LocalDateRange.html) – Basil Bourque Mar 21 '18 at 18:08

2 Answers2

1

You said in the comments that your inputs are "in date format", but that's a very vague description, because, technically speaking, Date objects don't have a format.

If your inputs are instances of java.util.Date, then just use the methods before, after and equals to compare the dates.

If you're using Java 8, the java.time API is a much better choice. You can either use a java.time.LocalDate or even a java.time.MonthDay, if you don't care about the year.

Both classes have comparison methods: isAfter, isBefore and equals, and a now() method to get the current date. With those, you have all the tools to implement your logic.

studx
  • 39
  • 3
0

Try something like this :

ArrayList<Date> dates = new ArrayList<>();

    DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    String dateString1 = "2017-02-15";
    String dateString2 = "2015-03-12";
    String dateString3 = "2016-07-19";
    Date date1 = null,date2=null,date3=null;

    try {
        date1 = sdf.parse(dateString1);
        date2 = sdf.parse(dateString2);
        date3 = sdf.parse(dateString3);
    } catch (ParseException e) {
        e.printStackTrace();
    }


    dates.add(date1);
    dates.add(date2);
    dates.add(date3);

    Collections.sort(dates, new Comparator<Date>() {
        public int compare(Date d1, Date d2) {
            return d1.compareTo(d2);
        }
    });
Benkerroum Mohamed
  • 1,867
  • 3
  • 13
  • 19
  • 2
    Please don’t teach the young ones to use the long outdated and notoriously troublesome `SimpleDateFormat` class. At least not as the first option. And not without any reservation. Today we have so much better in [`java.time`, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/) and its `DateTimeFormatter`. – Ole V.V. Mar 21 '18 at 10:25
  • 1
    For sure `java.time` it's a better approach – Benkerroum Mohamed Mar 21 '18 at 10:30