Ookami
Ookami

Reputation: 111

Comparing Dates with optional "comparison values"

I would like to compare the current date with the following optional variables which will be provided (multiple can be used): Year, Month, Day, Hours, Minutes, Seconds.

Possible comparison operators would be "before", "after" or "equals" - for single-variable comparisons a beforeOrEquals, afterOrEquals operator would be helpful as well.

Here are two examples how it would work:

If only the year is set, it would only check if the year is before/after the current year. If both the year and the month is set, it would consider both year and month in the comparisons.

The special thing with this comparison is if, for example, only the month is set, but not the year. In that case, the 21. February 2020 should be before the 21. March 2020 but AFTER the 21. January 2021.

For the current date, a LocalDateTime will be used. The comparison values (year, month etc.) are Integers. If they are -1, they should not be compared (= they are not provided).

This is how I'm currently doing the comparison:

    public final boolean compare(){
        //These values will need to be changed accordingly before the comparison. I have my own way of letting the user provide them as input.
        int year = -1;
        int month = -1;
        int day = -1;
        int hours = -1;
        int minutes = -1;
        int seconds = -1;
        String operation = "";
        String timeZone = null;

        final LocalDateTime currentTime = timeZone == null ? LocalDateTime.now() : LocalDateTime.now(timeZone.toZoneId());


        final LocalDateTime timeToCompare = LocalDateTime.of(
                year > -1 ? year  : currentTime.getYear(),
                month > -1 ? month : currentTime.getMonthValue(),
                day > -1 ? day : currentTime.getDayOfMonth(),
                hours > -1 ? hours : currentTime.getHour(),
                minutes > -1 ? minutes : currentTime.getMinute(),
                seconds > -1 ? seconds : currentTime.getSecond()
        );

        if(operation.equals("before")){
            if(!currentTime.isBefore(timeToCompare)){
                return false;
            }
        } else if(operation.equals("after")){
            if(!currentTime.isAfter(timeToCompare)){
                return false;
            }
        }
        return true;
    }

Is this a good way of implementing it, or is there a more performant way, since not every variable needs to be compared at all?

Upvotes: 0

Views: 481

Answers (1)

Bohemian
Bohemian

Reputation: 425013

I would apply the KISS principle:

record PartialDateTime (Integer year, Integer month, Integer day, Integer hour, Integer minute, Integer second) 
        implements Comparable<PartialDateTime> {

    public int compareTo(PartialDateTime other) {
        // your impl here
    }

    public boolean isBefore(PartialDateTime other) {
        return compareTo(other) < 0;
    }

    // etc for other truthy comparison methods
}

Upvotes: 2

Related Questions