techjogi
techjogi

Reputation: 61

How to get the Date Of Birth given number of years months and days of a person in java

Given year, month and days of a person, need to get Date of Birth - Example - 19 years 1 moth and 2 days 16-Sept-2010 (have calculated it manually, may not be accurate)

Upvotes: 1

Views: 1314

Answers (5)

deHaar
deHaar

Reputation: 18578

Basically, you can use the modern API for dates and times java.time and especially the class LocalDate. It has methods to add or subtract units of time, such as days, months and years.

public static void main(String[] args) {
    System.out.println("Imagine someone being 19 years, 1 months and 2 days old today...");
    LocalDate birthday = getBirthdayFromAge(19, 1, 2);
    System.out.println("Then this person was born on "
                        + birthday.format(DateTimeFormatter.ISO_DATE));
}

public static LocalDate getBirthdayFromAge(int years, int months, int days) {
    return LocalDate.now()
            .minusDays(days)
            .minusMonths(months)
            .minusYears(years);
}

This outputs

Imagine someone being 19 years, 1 months and 2 days old today...
Then this person was born on 2000-09-15

Upvotes: 2

Michał Krzywański
Michał Krzywański

Reputation: 16910

I would go with java.time.LocalDate and java.time.Period class. Calling minus methods might not be optimal as it will create new object for every method invocation (classes like LocalDate, LocalDateTime are immutable) :

Period period = Period.of(19, 1, 2); //period of 19 years, 1 month, 2 days
LocalDate birthday = LocalDate.now().minus(period); // compute the birthday

String formattedDate = birthday.format(DateTimeFormatter.ofPattern("dd-MMMM-YYYY", Locale.UK));
System.out.println(formattedDate);

The output is :

15-September-2000

Upvotes: 2

Vimal
Vimal

Reputation: 404

Here is quick fix for you. Please check following code.

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;

public class Birthdate {
    public static void main(String[] args) {
        Birthdate calUsage = new Birthdate();
        calUsage.subtractTime();
    }

    private void subtractTime() {
        Calendar calendar = new GregorianCalendar();

        String pattern = "yyyy-MMMM-dd";
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        String date = sdf.format(calendar.getTime());
        System.out.println("Current Date::" + date);

        calendar.add(Calendar.MONTH, -1);
        calendar.add(Calendar.DAY_OF_YEAR, -2);
        calendar.add(Calendar.YEAR, -19);
        date = sdf.format(calendar.getTime());
        System.out.println("Birthdate ::" + date);

    }
}

Hope this solution works.

Upvotes: 1

Nick
Nick

Reputation: 825

You should to transform year to Timestamp,month to Timestamp, day to Timestamp. Diff this with current timestamp and you will get birth date Timestamp

Upvotes: 1

Michel_T.
Michel_T.

Reputation: 2821

LocalDateTime.now().minusYears(years).minusMonths(months).minusDays(days)

Upvotes: 4

Related Questions