计算当前年龄;使用Java

时间:2011-10-26 08:56:35

标签: java nsdateformatter jodatime

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

我可以单独询问年,月和日,但更喜欢(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.");
  }
}

4 个答案:

答案 0 :(得分:3)

以下是您如何读入和解析输入日期的提示:

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期。

答案 1 :(得分:1)

如果你想使用JodaTime:

String dob = myScanner.nextLine();
DateTimeFormatter format = DateTimeFormatter.forPattern("MM/dd/yyyy");
DateTime dateTimeOfBirth = DateTime.parse(dob, format);

year = dateTimeOfBirth.getYear();
// Etc

答案 2 :(得分:1)

试试这个,它将年龄放入名为age的表单字段中,称为“表单”,假设你调用了你的文本框(输入了DOB)“dob”

var d =document.getElementById('dob').value.split('/'); 
var today = new Date(); 
var bday = new Date(d[2],d[1],d[0]);
var age = today.getFullYear() - bday.getFullYear();  
if(today.getMonth() < bday.getMonth() || (today.getMonth() == bday.getMonth() && today.getDate() < bday.getDate()))  
{
     t = age--;  
}
else {
t = age
}
form.age.value = t; 
} 

答案 3 :(得分:0)

你可能需要这样的东西:

SimpleDateFormat sdf = new SimpleDateFormat("dd/mm/yyyy", Locale.US);
Date date = sdf.parse(timestamp);
Calendar cd = Calendar.getInstance();
cd.setTime(date);