Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

375
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda