I need to take a birthday entered by the user (preferably in dd/mm//yyyy
format) and find their age, based on todays date. Could someone explain to me the proce
Using the java.time framework built into Java 8 and later. Example is copy pasted directly from Tutorial (with small modifications).
import java.time.Period
import java.time.LocalDate
import java.time.format.DateTimeFormatter
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("d/M/yyyy");
LocalDate today = LocalDate.now();
LocalDate birthday = LocalDate.parse("1/1/1960", formatter);
Period p = Period.between(birthday, today);
System.out.println("You are " + p.getYears() + " years, " + p.getMonths() +
" months and " + p.getDays() +
" days old.");
The code produces output similar to the following:
You are 53 years, 4 months and 29 days old.
In my opinion it doesn't make sense to output hour, minutes and seconds because you probably won't have such precise data in your DB. That is why example uses LocalDate
instead of LocalDateTime
.
public class Main {
public static void main(String[] args) {
LocalDateTime dateTime = LocalDateTime.of(2018, 12, 27, 11, 45, 0);
Duration showSeconds = AgeCalculator.calculateAgeDuration(dateTime, LocalDateTime.now());
TimeConverter.calculateTime(showSeconds.getSeconds());
}
}
public class AgeCalculator {
public static Duration calculateAgeDuration(LocalDateTime dayBefore, LocalDateTime currentDay) {
return Duration.between(dayBefore, currentDay);
}
}
public class TimeConverter {
public static void calculateTime(long timeSeconds) {
long days = timeSeconds / 86400; // 24*60*60
long hours = timeSeconds / 3600;
long minutes = (timeSeconds % 3600) / 60;
long seconds = (timeSeconds % 3600) % 60;
System.out.println("Days: " + days);
System.out.println("Hours: " + hours);
System.out.println("Minutes: " + minutes);
System.out.println("Seconds: " + seconds);
}
}
Days: 0 Hours: 4 Minutes: 30 Seconds: 29
There are a couple of ways. I would use joda-time, using the Period class to represent the delta between today and the date of birth. It provides exactly the capability you want.
If you don't want to deal with a 3rd party library, then get Date
objects representing the two dates in question and call getTime()
on both, subtract the latest from the earliest and you'll have the delta between the dates in milliseconds. The maths to convert that into years/months/days etc is trivial.* Something like:
delta /= 1000; // convert ms to s
if (delta > 60 * 60 * 24 * 365) // number of seconds in a year
int years = delta / (60 * 60 * 24 * 365) // integer division to get the number of years
delta %= (60 * 60 * 24 * 365) // assign the remainder back to delta
// repeat ad nauseum
I'd suggest using Joda time. It's a much better API than what's included in the JDK.
Create an Instant representing when the person was born, another representing the current time, and use those two Instant
s to create a Period. From there it's easy to get the fields you need using the methods provided in the Period
class.