Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Date implementation with unknown day, month and/or year

Some real people do not know what day, month or even year they were born.

This can pose a problem for systems that require a full date of birth.

In the Netherlands the decision was made to code these birthdates with unknowns. The following is the birthdate of someone born in 1975, exact date unknown:

00-00-1975

How can I handle this in Java? I'm not asking about parsing a string of "00-00-1975" but an actual date implementation that supports unknown (maybe null?) values.

Thankfully, decision-makers wisened up in 2014, and now people with an unknown date of birth are given a made-up date. https://www.binnenlandsbestuur.nl/bestuur-en-organisatie/nieuws/niet-bestaande-geboortedatum-00-00-jj-blijft.9484714.lynkx

like image 825
Ivana Avatar asked Mar 02 '23 09:03

Ivana


2 Answers

No, no such thing. You can implement your own, e.g.

interface Birthday {
    Optional<LocalDate> fullDate();
    Year year();
}

class UnknownBirthday implements Birthday {
    private final Year year;
    // ctor

    @Override
    public Year year() {
        return year;
    }

    @Override
    public Optional<LocalDate> fullDate() {
        return Optional.empty();
    }
}

class KnownBirthday implements Birthday {
    private final LocalDate date;
    // ctor
    
    @Override
    public Optional<LocalDate> fullDate() {
        return Optional.of(date);
    }

    @Override
    public Year year() {
        return Year.of(date.getYear());
    }
}
like image 179
Michael Avatar answered Mar 05 '23 18:03

Michael


One option is to exploit the TemporalAccessor interface. Nearly all of the date and time classes of java.time implement this interface and certainly those classes that you would use for your uncertain birthdays. Declare a variable a TemporalAccessor, and you may assign a Year or a LocalDate or some other type to it.

Some programmers would be tempted to use instanceof a lot with such a variable, which may be considered not so nice. Fortunately for many purposes it also isn’t necessary. The isSupported method of TemporalAccessor can be used in most situations.

For a demonstration:

    // Example birthdays ordered from the best known to the completely unknown
    TemporalAccessor[] birthdays = {
            LocalDate.of(1955, Month.MARCH, 22),
            YearMonth.of(1978, Month.MAY),
            Year.of(1969),
            IsoEra.CE
    };

    for (TemporalAccessor birthday : birthdays) {
        String yearString = birthday.isSupported(ChronoField.YEAR)
                ? String.valueOf(birthday.get(ChronoField.YEAR))
                : "N/A";
        String monthString = birthday.isSupported(ChronoField.MONTH_OF_YEAR)
                ? Month.from(birthday).toString()
                : "N/A";
        String dateString = birthday.isSupported(ChronoField.DAY_OF_MONTH)
                ? String.valueOf(birthday.get(ChronoField.DAY_OF_MONTH))
                : "N/A";

        System.out.format("%-10s %4s %-9s %3s%n", birthday, yearString, monthString, dateString);
    }

Output from this snippet is:

1955-03-22 1955 MARCH      22
1978-05    1978 MAY       N/A
1969       1969 N/A       N/A
CE          N/A N/A       N/A 

For a nicer interface you may wrap the TemporalAccessor in a home-made class. Your class could have getters for Year and LocalDate, for example, which could return an empty Optional or null in the cases where there are not enough data. You can think of more convenience methods. You know your requirements best, so I am leaving the details of the design to yourself.

like image 24
Ole V.V. Avatar answered Mar 05 '23 18:03

Ole V.V.