Vladik
Vladik

Reputation: 45

How to compare two dates in Java and then compare the result vs another date?

Noob here! First I am trying to compare dateOne and dateTwo with each other, then which ever that has the most recent date (> 0) take it and compare that versus the cutOffDate.

If its after cutOffDate, set that to be FinalDate, else set cutOffDate to be FinalDate. How can I achieve this?

I'm not sure if I'm on the right track here, I am getting the following error with dateOne.compareTo(dateTwo):

The method compareTo(String) in the type String is not applicable for the arguments (Date)

public DateServiceImpl process(final DateServiceImpl item) throws Exception {

    final Date dateOne = item.getDateOne();
    final Date dateTwo = item.getDateTwo();

    final BigDecimal amountOne= item.getAmountOne();
    final BigDecimal amountTwo= item.getAmountTwo();

    SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
    String cutOffDate = "01/01/2020";

    Date cDate = sdf.parse(cutOffDate);
    if ((amountOne.compareTo(BigDecimal.ZERO) > 0) && (amountTwo.compareTo(BigDecimal.ZERO) > 0)){
        if (dateOne.compareTo(dateTwo) <= 0) {
             item.setFinalDate(cDate);
        }
        return null;
    }
}

Upvotes: 3

Views: 292

Answers (2)

Anonymous
Anonymous

Reputation: 86276

LocalDate from java.time and Collections.max()

Do use java.time, the modern Java date and time API, for your date work. First, java.time allows to declare the formatter once and for all:

private static final DateTimeFormatter DATE_FORMATTER
        = DateTimeFormatter.ofPattern("MM/dd/yyyy");

I understand that the final date should be the latest of dateOne, dateTwo and the cut-off date. Given that DateServiceImpl uses LocalDate instead of Date, the logic can be simplified:

    LocalDate dateOne = item.getDateOne();
    LocalDate dateTwo = item.getDateTwo();

    String cutOffDate = "01/01/2020";

    LocalDate cDate = LocalDate.parse(cutOffDate, DATE_FORMATTER);

    LocalDate finalDate = Collections.max(Arrays.asList(dateOne, dateTwo, cDate));

    item.setFinalDate(finalDate);

I have left out the final declarations and the amount logic since they weren’t relevant to the question asked; you can put those back in yourself.

A LocalDate is a date without time of day. I thought that this was what you needed, please check yourself.

Edit: as Basil Bourque suggests in a comment, if using Java 9 or later, you will probably prefer List.of() over Arrays.asList():

    LocalDate finalDate = Collections.max(List.of(dateOne, dateTwo, cDate));

For situations where this is not enough LocalDate also has methods isBefore and isAfter for comparisons.

Collections.max() works with Date too

If you cannot afford to upgrade DateServiceImpl to java.time just now, you may use Collections.max() on three old-fashioned Date objects in the same way.

Collections.max() also comes in an overloaded version that takes Comparator as its second argument. Since both LocalDate and Date implement Comparable we don’t need that here.

Links

Upvotes: 5

parthlr
parthlr

Reputation: 426

You're on the right track. You just need another comparison and you should be good.

if (dateOne.compareTo(dateTwo) > 0) {
    if (dateOne.compareTo(cDate) > 0) {
        item.setFinalDate(dateOne);
    } else {
        item.setFinalDate(cDate);
    }
} else {
    if (dateTwo.compareTo(cDate) > 0) {
        item.setFinalDate(dateTwo);
    } else {
        item.setFinalDate(cDate);
    }
}

You needed to check each case of which order the dates come in (check if dateOne comes before dateTwo and check if dateOne comes after dateTwo). Then, for each of those checks, you have to check whether the most recent date comes before or after the cutoff date.

Upvotes: 4

Related Questions