用Java计算出生日期

时间:2015-09-07 16:52:55

标签: java date

我正在尝试创建一个程序,计算出你生活了多少天。以下代码是我所要做的。变量是否存在某种日期类型来存储您的出生日期?

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;
    }        

}

1 个答案:

答案 0 :(得分:2)

如果你正在使用Java 8,内置的LocalDate是最容易使用的。

如果不是在Java 8上,最简单的方法是使用Joda-Time库。

否则,您必须使用Calendar对象。使用Calendar对象更复杂,因为您要比较的两个日期(生日与今天)可能位于不同的daylight savings time"区域"。这可以通过计算两个(纯)日期之间的毫秒数除以24小时,舍入结果使得23,24和25小时都变为1天来实现。

以下是3个实现,分别使用Java 8,Joda-Time和Calendar。由于名称冲突,代码使用完全限定。

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;
}

使用以下方法测试代码:

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));
}

生成此输出:

Today is: 2015-09-07
Age (in days) of Scott McNealy, co-founder of Sun Microsystems (born November 13, 1954):
22213
22213
22213