gpt4 book ai didi

java - 计算当前年龄;使用Java

转载 作者:行者123 更新时间:2023-11-29 08:11:30 29 4
gpt4 key购买 nike

我正在尝试计算用户的当前年龄.. 但是使用我想使用的格式给我带来了一些问题。

我可以单独询问年、月和日,但更喜欢 (mm/dd/yyyy)

//Need help turning (mm/dd/yyyy) that the user inputs into:

//yyyy;

//mm;

// dd;


package org.joda.time;

import java.util.Calendar;
import java.util.GregorianCalendar;//needed for leap year
import java.util.Scanner;
import java.io.*;

public class AgeCalculation{
public static void main(String[] args) throws IOException{




int day = 1, month = 0, year = 1, ageYears, ageMonths, ageDays;
Scanner myScanner = new Scanner(System.in);

BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
Calendar cd = Calendar.getInstance();
try{


System.out.print("Enter your Date of Birth.(mm/dd/yyyy): ");
Dob = myScanner.nextLine() ;

// how to get year , month and date from (mm/dd/yyyy) format?


year = ;
if(year > cd.get(Calendar.YEAR)){
System.out.print("Invalid date of birth.");
System.exit(0);
}
month =
if(month < 1 || month > 12){
System.out.print("Please enter monthe between 1 to 12.");
System.exit(0);
}
else{
month--;
if(year == cd.get(Calendar.YEAR)){
if(month > cd.get(Calendar.MONTH)){
System.out.print("Invalid month!");
System.exit(0);
}
}
}
if(month == 0 || month == 2 || month == 4 || month == 6 || month == 7 ||
month == 9 || month == 11){
if(day > 31 || day < 1){

//leap year data below

System.out.print("Please enter day between 1 to 31.");
System.exit(0);
}
}
else if(month == 3 || month == 5 || month == 8 || month == 10){
if(day > 30 || day < 1){
System.out.print("Please enter day between 1 to 30.");
System.exit(0);
}
}
else{
if(new GregorianCalendar().isLeapYear(year)){
if(day < 1 || day > 29){
System.out.print("Please enter day between 1 to 29.");
System.exit(0);
}
}
else if(day < 1 || day > 28){
System.out.print("Please enter day between 1 to 28.");
System.exit(0);
}
}
if(year == cd.get(Calendar.YEAR)){
if(month == cd.get(Calendar.MONTH)){
if(day > cd.get(Calendar.DAY_OF_MONTH)){
System.out.print("Invalid date!");
System.exit(0);
}
}
}
}
catch(NumberFormatException ne){
System.out.print(ne.getMessage() + " is not a legal entry!");
System.out.print("Please enter number.");
System.exit(0);
}
Calendar bd = new GregorianCalendar(year, month, day);
ageYears = cd.get(Calendar.YEAR) - bd.get(Calendar.YEAR);
if(cd.before(new GregorianCalendar(cd.get(Calendar.YEAR), month, day))){
ageYears--;
ageMonths = (12 - (bd.get(Calendar.MONTH) + 1)) + (bd.get(Calendar.MONTH));
if(day > cd.get(Calendar.DAY_OF_MONTH)){
ageDays = day - cd.get(Calendar.DAY_OF_MONTH);
}
else if(day < cd.get(Calendar.DAY_OF_MONTH)){
ageDays = cd.get(Calendar.DAY_OF_MONTH) - day;
}
else{
ageDays = 0;
}
}
else if(cd.after(new GregorianCalendar(cd.get(Calendar.YEAR), month, day))){
ageMonths = (cd.get(Calendar.MONTH) - (bd.get(Calendar.MONTH)));
if(day > cd.get(Calendar.DAY_OF_MONTH))
ageDays = day - cd.get(Calendar.DAY_OF_MONTH) - day;
else if(day < cd.get(Calendar.DAY_OF_MONTH)){
ageDays = cd.get(Calendar.DAY_OF_MONTH) - day;
}
else
ageDays = 0;
}
else{
ageYears = cd.get(Calendar.YEAR) - bd.get(Calendar.YEAR);
ageMonths = 0;
ageDays = 0;
}
System.out.print("Age of the person : " + ageYears + " year, " + ageMonths +
" months and " + ageDays + " days.");
}
}

最佳答案

这里有一个关于如何读入和解析输入日期的提示:

SimpleDateFormat f = new SimpleDateFormat("MM/dd/yyyy"); //note that mm is minutes, so you need MM here
Scanner s = new Scanner( System.in );

String dateLine = s.nextLine();
try
{
Date d = f.parse( dateLine );
System.out.println(d);
}
catch( ParseException e )
{
System.out.println("please enter a valid date in format mm/dd/yyyy");
}

这只是一个例子,应该能让您开始正确阅读日期。

要获取两个日期之间的年、日和月,我仍然建议使用 JodaTime,因为这会让生活变得更轻松。

使用标准的 Java 工具,这可以帮助您:

Calendar c = Calendar.getInstance();
c.setTimeInMillis( System.currentTimeMillis() - d.getTime() );
System.out.println( c.get( Calendar.YEAR ) - 1970 ); //since year is based on 1970, subtract that
System.out.println( c.get( Calendar.MONTH ) );
System.out.println( c.get( Calendar.DAY_OF_MONTH ) );

请注意,您需要从较大的日期中减去较小的日期,即差值必须为正(从当前时间减去出生日期时应该为真)。

这将使 2011 年 1 月 1 日和 2011 年 10 月 26 日之间相差 0 年 9 个完整月和 26 天,并且在 1990 年 10 月 26 日和 10 月之间相差 21 年零 1 天(因为我们已经从这一天开始) 2011 年 26 日。

关于java - 计算当前年龄;使用Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7900587/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com