简体   繁体   中英

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

No, no such thing. You can implement your own, eg

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());
    }
}

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/AN/A CE N/AN/AN/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.

Joda Time library has Partial class which can be used to create dates with unknown month / day etc.

Partial is an immutable partial datetime supporting any set of datetime fields.

A Partial instance can be used to hold any combination of fields. The instance does not contain a time zone, so any datetime is local.

    Partial date = new Partial();
    
    // prints 1975
    System.out.println( date.with(year(), 1975) );

    // prints 1975-03
    System.out.println( date.with(monthOfYear(), 3).with(year(), 1975));

    // prints 1975-03-28
    System.out.println( date.with(dayOfMonth(), 28).with(monthOfYear(), 3).with(year(), 1975));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM