Reputation: 41
I'm trying to create a program which works out how many days you've lived for. The code below is as far as I've got to. Is there a certain date type for a variable to store your date of birth?
public static void main(String[] args) {
Scanner Input = new Scanner(System.in);
Date CurrentDate = new Date();
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
int day;
int month;
int year;
int noofDaysOld = 0;
System.out.println("Age Calculator");
System.out.println();
System.out.println("Current Date: "+ dateFormat.format(CurrentDate));
System.out.println("Find out how many days you have lived for");
System.out.println();
System.out.println("Input your date of birth");
System.out.println("Day (of month e.g. 4th)");
day = Input.nextInt();
System.out.println("Month (no e.g. 1: January)");
month = Input.nextInt();
System.out.println("Year");
year = Input.nextInt();
System.out.println();
noofDaysOld = CalculateDayAge(day, month, year);
System.out.println("You age in days is: "+noofDaysOld);
}
public static int CalculateDayAge(int dayNo, int monthNo, int yearAD){
Date DateofBirth;
DateofBirth;
return DayAge;
}
}
Upvotes: 2
Views: 4781
Reputation: 159215
If you're using Java 8, the built-in LocalDate
is the easiest to use.
If not on Java 8, the easiest is to use the Joda-Time library.
Otherwise, you have to use a Calendar
object. Using the Calendar object is more complicated, because the two dates you'll be comparing (birthday vs today) may be in different daylight savings time "zones". This can be accomodated by calculating the number of milliseconds between the two (pure) dates and dividing by 24 hours, rounding the result such that both 23, 24, and 25 hours becomes 1 day.
Here are the 3 implementations, using Java 8, Joda-Time, and Calendar, respectively. Code uses full qualification because of name clash.
public static int calculateDayAge1(int dayNo, int monthNo, int yearAD) {
// Use Java 8 LocalDate
java.time.LocalDate birthDate = java.time.LocalDate.of(yearAD, monthNo, dayNo);
java.time.LocalDate today = java.time.LocalDate.now();
long days = birthDate.until(today, java.time.temporal.ChronoUnit.DAYS);
return (int)days;
}
public static int calculateDayAge2(int dayNo, int monthNo, int yearAD) {
// Use Joda-Time LocalDate
org.joda.time.LocalDate birthDate = new org.joda.time.LocalDate(yearAD, monthNo, dayNo);
org.joda.time.LocalDate today = org.joda.time.LocalDate.now();
org.joda.time.Days days = org.joda.time.Days.daysBetween(birthDate, today);
return days.getDays();
}
public static int calculateDayAge3(int dayNo, int monthNo, int yearAD) {
// Get today/now to millisecond precision
java.util.Calendar cal = java.util.Calendar.getInstance();
// Get millis for today at midnight (make it a pure date)
int todayYear = cal.get(java.util.Calendar.YEAR);
int todayMonth = cal.get(java.util.Calendar.MONTH);
int todayDay = cal.get(java.util.Calendar.DAY_OF_MONTH);
cal.clear();
cal.set(todayYear, todayMonth, todayDay);
long todayMillis = cal.getTimeInMillis();
// Get millis for birthdate
cal.set(yearAD, monthNo - 1, dayNo);
long birthdayMillis = cal.getTimeInMillis();
// Calculate number of 24 hour days between birthdate and today,
// rounding the result to account for daylight savings time discrepancies
final long millisPerDay = 24 * 60 * 60 * 1000; // 86400000
long days = (todayMillis - birthdayMillis + millisPerDay / 2) / millisPerDay;
return (int)days;
}
Testing the code using:
public static void main(String[] args) {
System.out.println("Today is: " + java.time.LocalDate.now());
System.out.println("Age (in days) of Scott McNealy, co-founder of Sun Microsystems (born November 13, 1954):");
System.out.println(calculateDayAge1(13, 11, 1954));
System.out.println(calculateDayAge2(13, 11, 1954));
System.out.println(calculateDayAge3(13, 11, 1954));
}
produces this output:
Today is: 2015-09-07
Age (in days) of Scott McNealy, co-founder of Sun Microsystems (born November 13, 1954):
22213
22213
22213
Upvotes: 2