Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

373
Views
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

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

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());
    }
}
over 4 years ago · Santiago Trujillo Report

0

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.

over 4 years ago · Santiago Trujillo Report

0

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));
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!