-3

I have 3 sets of start date and end dates
1st - start date (01/25/2017) and an end date (02/26/2017)
2nd - set of (01/25/2017) and an end date (02/26/2017)
3rd - set (03/25/2017) and an end date (04/26/2017)

I want to write a Java program to compare the 3 sets and give me the two sets which are equal:

((01/25/2017) - (02/26/2017))

Note that start date and end date comes from two different variables.

double-beep
  • 5,031
  • 17
  • 33
  • 41
  • 3
    The question is currently off-topic, please post the code you tried to solve the problem. – Jed Burke Aug 03 '18 at 23:02
  • 2
    [Why is “Can someone help me?” not an actual question?](https://meta.stackoverflow.com/q/284236/5221149) – Andreas Aug 03 '18 at 23:04
  • 2
    Okay, so your question, thus far, simply boils down to solving “how to compare dates”, once you can do that, you just need to repeat the process over you sets – MadProgrammer Aug 03 '18 at 23:14
  • I rather think the core of the question is “how to design a program to manage and compare date intervals?” However, Stack Overflow is not a design service, so the question (as I understand it) isn’t really suited for this place, sorry. – Ole V.V. Aug 04 '18 at 13:53

2 Answers2

1

org.threeten.extra.LocalDateRange::equals

See the ThreeTen-Extra project for the LocalDateRange class with its equals method. This method compares one LocalDateRange with another ensuring that the pair of dates are the same.

That class depends on the java.time.LocalDate class built into Java 8 and later. That class has been covered many hundreds of times on this site. So search Stack Overflow for further info.

LocalDateRange.of(
    LocalDate.of( 2017 , Month.JANUARY , 25 ) ,
    LocalDate.of( 2017 , Month.FEBRUARY , 26 )      // Returns a `LocalDate` object.
)                                                   // Returns a `LocalDateRange` object.
.equals(
    LocalDateRange.of(
        LocalDate.of( 2017 , Month.MARCH , 25 ) ,
        LocalDate.of( 2017 , Month.APRIL , 26 ) 
    )
)

false

ChronoUnit.DAYS::between

If you meant to compare the number of days elapsed, use ChronoUnit enum.

ChronoUnit.DAYS.between(
    LocalDate.of( 2017 , Month.JANUARY , 25 ) ,
    LocalDate.of( 2017 , Month.FEBRUARY , 26 ) 
)                                                   // Returns a `long` integer primitive.
 == 
ChronoUnit.DAYS.between(
    LocalDate.of( 2017 , Month.MARCH , 25 ) ,
    LocalDate.of( 2017 , Month.APRIL , 26 ) 
)

About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.* classes.

Where to obtain the java.time classes?

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
1

One way to do this is to create a custom class (MyDateSets) which holds a set of start and end dates. The class's equals method (override the Object's equals) specifies the date set equality.

The example code:

import java.time.*;
import java.time.format.*;
public class CompareDateSets {
    public static void main(String [] args) {
        // Parse date strings to LocalDate objects
        // Make 3 set sof them - the input
        DateTimeFormatter parser = DateTimeFormatter.ofPattern("MM/dd/yyyy");
        LocalDate start1 = LocalDate.parse("01/25/2017", parser);
        LocalDate end1 = LocalDate.parse("01/25/2017", parser);
        LocalDate start2 = LocalDate.parse("01/25/2017", parser);
        LocalDate end2 = LocalDate.parse("01/25/2017", parser);
        LocalDate start3 = LocalDate.parse("03/25/2017", parser);
        LocalDate end3 = LocalDate.parse("04/26/2017", parser);
        // Create MyDateSets objects - 3 sets of input
        MyDateSets set1 = new MyDateSets(start1, end1);
        MyDateSets set2 = new MyDateSets(start2, end2);
        MyDateSets set3 = new MyDateSets(start3, end3);
        // Compare each set and print the comparison result
        System.out.println(set1.equals(set2)); // true
        System.out.println(set1.equals(set3));  // false
        System.out.println(set2.equals(set3));  // false
    }
}

/*
 * Custom class has a set of the start and end dates.
 * Overrides Object class's equals method to define
 * the date set's equality.
 */
class MyDateSets {
    private LocalDate start;
    private LocalDate end;
    public MyDateSets(LocalDate start, LocalDate end) {
        this.start = start;
        this.end = end;
    }
    public LocalDate getStart() {
        return start;
    }
    public LocalDate getEnd() {
        return end;
    }
    @Override
    public boolean equals(Object o) {
        MyDateSets dates = (MyDateSets) o;
        if ((dates.getStart().equals(start)) &&
                (dates.getEnd().equals(end))) {
            return true;
        }
        return false;
    }
}
prasad_
  • 12,755
  • 2
  • 24
  • 36