计算年,月,日,小时,分钟和秒的年龄

Chr*_* V. 4 java date

我需要用户输入的生日(最好是dd/mm//yyyy格式)并根据今天的日期查找他们的年龄.有人可以向我解释我应该找到的这个过程吗?我需要以以下格式打印出来:

"你是19岁,4个月,12天,8小时,44分钟,39秒."

我对如何从另一个日期减去日期感到困惑,以及我将如何分别引用每个部分(年,月,日,小时等).

我现在的代码供参考:

import java.sql.Date;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Scanner;

public class Driver {

    public Driver(){}

    public static void main( String[] args){
    Driver menu = new Driver();
    MenuOptions option;
    do{
        menu.displayMenu();
        option = menu.getResponse();
        menu.runOption(option);
    }while(option != MenuOptions.Quit);  
    }

    private enum MenuOptions{
    AgeCalc("Calculate your age"),
        AnniversaryCalc("Calculate time until specified date"),
        AgeDifference("Find the time between two specified dates"),
        Quit("Exit the program");

    private String value;

    private MenuOptions(String value){
        this.value = value;
    }

    public String toString(){
        return value;
    }
    }

    public void displayMenu(){
    for(MenuOptions option : MenuOptions.values()){
        System.out.printf("%5d: %s\n", option.ordinal()+1, option);
    }
    }

    public MenuOptions getResponse(){
    int value = -1;
    Scanner input = new Scanner(System.in);

    do{
        System.out.println("> ");
        String response = input.nextLine();

        if(response.matches("\\d+")){
        value = Integer.parseInt(response);
        if(value < 1 || value > MenuOptions.values().length){
            value = -1;
            System.out.println("Unknown response, please enter a number between 1 and " +MenuOptions .values().length);
        }
        }

    }while(value <=0);

    return MenuOptions.values()[value-1];
    }

    public void runOption(MenuOptions option){
    Scanner in = new Scanner(System.in);

    switch(option){
    case AgeCalc:
        System.out.println("Please enter your birthday mm/DD/yyyy).");
        System.out.println(">");

        DateFormat df = new SimpleDateFormat("mm/DD/yyyy");

        try
        {
            java.util.Date birthday = df.parse(in.nextLine());           
            System.out.println("Today = " + df.format(birthday));
        } catch (ParseException e)
        {
            e.printStackTrace();
        }
        Calendar today = Calendar.getInstance();
        java.util.Date today = df.format();

        DateFormat simple = new SimpleDateFormat("dd/MM/yyyy");
        String birthdate = simple.format(in.nextLine());
        Date.parse(birthdate);

        System.out.println(birthdate.toString());
        break;
    case AnniversaryCalc:
        System.out.printf("PI: %.20f\n", Math.PI);
        break;
    case AgeDifference:
        for(int p=0; p <= 32; p++){
        System.out.println("2^" +p+" = " +Math.pow(2,p));
        }
    }
    }
}
Run Code Online (Sandbox Code Playgroud)

Bra*_*ace 5

我建议使用Joda时间.这是一个比JDK中包含的API更好的API.

创建一个即时代表当人诞生,另一个表示当前时间,并使用这两个Instants到创建一个周期.从那里,您可以使用课程中提供的方法轻松获取所需的字段Period.