以年,月,日,小时,分,秒计算年龄 [英] Calculate age in Years, Months, Days, Hours, Minutes, and Seconds

查看:252
本文介绍了以年,月,日,小时,分,秒计算年龄的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用用户输入的生日(最好在 dd / mm // yyyy 格式),并根据今天的日期找到自己的年龄。有人向我解释我应该怎么去找到这个过程?我需要以格式打印出来:

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 process I should go through to find this? I need to print it out in the format:


你是19年,4个月,12天,8小时,44分钟,和39秒。

"You are 19 years, 4 months, 12 days, 8 hours, 44 minutes, and 39 seconds old."

我只是有点困惑我如何从另一个日期减去一个日期,将分别引用每个部分(年,月,日,小时等)。

I'm just a little confused on how I would subtract a date from another date, and how I would reference each part (years, months, days, hours, etc) separately.

此刻我有代码参考:

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


推荐答案

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

I'd suggest using Joda time. It's a much better API than what's included in the JDK.

创建即时代表当该人出生时,另一个代表当前时间,并使用这两个 Instant 来创建一个期间。从那里可以很容易地使用 期间中提供的方法

Create an Instant representing when the person was born, another representing the current time, and use those two Instants to create a Period. From there it's easy to get the fields you need using the methods provided in the Period class.

这篇关于以年,月,日,小时,分,秒计算年龄的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆