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)
Asked
Active
Viewed 176 times
5 Answers
4
LocalDateTime.now().minusYears(years).minusMonths(months).minusDays(days)
![](../../users/profiles/1910483.webp)
Michel_T.
- 2,353
- 5
- 18
- 27
-
Or is it `LocalDateTime.now().minusDays(days).minusMonths(months).minusYears(years)`? May not be the same result. --- If I gave my age in year, months, and days, I would have gotten that by adding those numbers in that sequence to by birth date, so to correctly reverse that, you have to subtract the numbers in reverse order. – Andreas Oct 17 '19 at 12:21
2
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
![](../../users/profiles/1712135.webp)
deHaar
- 11,298
- 10
- 32
- 38
-
Or is it `LocalDate.now().minusDays(days).minusMonths(months).minusYears(years)`? May not be the same result. --- If I gave my age in year, months, and days, I would have gotten that by adding those numbers in that sequence to by birth date, so to correctly reverse that, you have to subtract the numbers in reverse order. – Andreas Oct 17 '19 at 12:23
-
@Andreas I didn't subtract anything from a `LocalDateTime` but from a `LocalDate` and I got the same result with reversed subtractions. Have you taken the same values? I'm interested… – deHaar Oct 17 '19 at 12:26
-
Sorry, copied comment from another answer which used `LocalDateTime`. Updated my comment. If you change number of days to 20, and since today is the 17th, it will cross to previous month, so the result depends on the number of days in the previous month, so the result is different depending on whether you subtract days before months, or months before days. E.g. `2019-10-17` (today) minus `19y 1m 20d` is `2000-08-28` with `.minusYears(years).minusMonths(months).minusDays(days)`, but it is `2000-08-27` with `.minusDays(days).minusMonths(months).minusYears(years)`. – Andreas Oct 17 '19 at 12:32
-
2
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
![](../../users/profiles/4956907.webp)
michalk
- 10,989
- 2
- 22
- 46
-
2Note that `localDate.minus(Period)` is actually broken. If birth date is `2000-08-27`, and today is `2019-10-17`, then person is `19 year, 1 month, and 20 days` old. With `birthDate = LocalDate.of(2000, 8, 27)` and `age = Period.of(19, 1, 20)`, that works out right, i.e. `birthDate.plus(age)` is `2019-10-17`, but note that `birthDate.plus(age).minus(age)` is *not* the birth date, but `2000-08-28`. --- `minus(age)` is not the reverse of `plus(age)`, it is actually the same as `plus(age.negated())`. – Andreas Oct 17 '19 at 12:47
-
1@Andreas you are right. This is somewhat interesting. I have found [this](https://stackoverflow.com/questions/41945704/adding-and-subtracting-period-from-localdate-doesnt-produce-the-same-date) and [that](https://stackoverflow.com/questions/43471207/localdate-minus-a-period-get-wrong-result) threads which explain why it happens. – michalk Oct 17 '19 at 13:25
1
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
![](../../users/profiles/9903848.webp)
Nick
- 678
- 2
- 13
1
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.
![](../../users/profiles/4264288.webp)
Vimal
- 420
- 6
- 28