When i run the code and put in my info it always gives me the same month
Ex. Year of birth: 2001
Month Of Birth: 5
Day of birth: 23
output (example not real) 11 years, 11 months and so and so days.....
u use a different month, same month output...... 11
Thanks, if you can fix this :)
according to me this
ageMonths = (12 - (bd.get(Calendar.MONTH) + 1)) + (bd.get(Calendar.MONTH));
should be
ageMonths = (12 - (bd.get(Calendar.MONTH) + 1)) + (cd.get(Calendar.MONTH)+1);
am i right????
import java.io.*;
class AgeCalculator{
public static void main(String args[])
{
InputStreamReader ins=new InputStreamReader(System.in);
BufferedReader hey=new BufferedReader(ins);
try{
System.out.println("Please enter your name: ");
String name=hey.readLine();
System.out.println("Please enter your birth date: ");
String date=hey.readLine();
System.out.println("please enter your birth month:");
String month=hey.readLine();
System.out.println("please enter your birth year:");
String year=hey.readLine();
System.out.println("please enter current year:");
String cYear=hey.readLine();
int bDate = Integer.parseInt(date);
int bMonth = Integer.parseInt(month);
int bYear = Integer.parseInt(year);
int ccYear=Integer.parseInt(cYear);
int age;
age=ccYear-bYear;
int totalMonth=12;
int yourMonth=totalMonth-bMonth;
System.out.println(" Hi " + name + " your are " + age + " years " + yourMonth + " months old ");
}
catch(IOException err)
{
System.out.println("");
}
}
}
Very Good!Adilson Victor April 18, 2011 at 8:33 PM
Finally I've found something useful in the internet about dates in java....Thanks for this code.
Errorskarthikeyan December 27, 2011 at 3:42 PM
working for some inputs. showing errors for some inputs ex., 1988,10,28 - shows days in negative
Bug in the age methodM Asif Iqbal January 10, 2012 at 3:41 PM
There is a bug in the above method. Check with this method. "1953-09-07"
Error In Codezach January 28, 2012 at 8:15 AM
When i run the code and put in my info it always gives me the same month Ex. Year of birth: 2001 Month Of Birth: 5 Day of birth: 23 output (example not real) 11 years, 11 months and so and so days..... u use a different month, same month output...... 11 Thanks, if you can fix this :)
changesiddhesh February 23, 2012 at 6:17 PM
according to me this ageMonths = (12 - (bd.get(Calendar.MONTH) + 1)) + (bd.get(Calendar.MONTH)); should be ageMonths = (12 - (bd.get(Calendar.MONTH) + 1)) + (cd.get(Calendar.MONTH)+1); am i right????
calculate age using javaDanny March 27, 2012 at 6:56 PM
I found your example guite helpful to me,I wish i could see more examples in your website
doubtnarendra May 10, 2012 at 10:45 AM
if i give the date as jan 21,1980, it is showing days as negative value..
One More Example ProgramSurendar S August 9, 2012 at 4:46 PM
import java.io.*; class AgeCalculator{ public static void main(String args[]) { InputStreamReader ins=new InputStreamReader(System.in); BufferedReader hey=new BufferedReader(ins); try{ System.out.println("Please enter your name: "); String name=hey.readLine(); System.out.println("Please enter your birth date: "); String date=hey.readLine(); System.out.println("please enter your birth month:"); String month=hey.readLine(); System.out.println("please enter your birth year:"); String year=hey.readLine(); System.out.println("please enter current year:"); String cYear=hey.readLine(); int bDate = Integer.parseInt(date); int bMonth = Integer.parseInt(month); int bYear = Integer.parseInt(year); int ccYear=Integer.parseInt(cYear); int age; age=ccYear-bYear; int totalMonth=12; int yourMonth=totalMonth-bMonth; System.out.println(" Hi " + name + " your are " + age + " years " + yourMonth + " months old "); } catch(IOException err) { System.out.println(""); } } }
error....X August 14, 2012 at 11:41 AM
There is some mistake in the code while calculating the months.. But it happens only in some cases.... This will solve the issue.. Calendar cd = Calendar.getInstance(); Calendar bd = new GregorianCalendar(birthYear, birthMonth, birthDate); year = cd.get(Calendar.YEAR) - bd.get(Calendar.YEAR); if(cd.before(new GregorianCalendar(cd.get(Calendar.YEAR), birthMonth, birthDate))) { year--; if(birthMonth> cd.get(Calendar.MONTH)) { month=12 - birthMonth + cd.get(Calendar.MONTH); if(birthDate > cd.get(Calendar.DAY_OF_MONTH)) { month--; } } else { month = (12 - (bd.get(Calendar.MONTH) + 1)) + (bd.get(Calendar.MONTH)); } } else if(cd.after(new GregorianCalendar(cd.get(Calendar.YEAR), birthMonth, birthDate))) { month = cd.get(Calendar.MONTH) - (bd.get(Calendar.MONTH)); if(birthDate > cd.get(Calendar.DAY_OF_MONTH)) { month--; } } else { year = cd.get(Calendar.YEAR) - bd.get(Calendar.YEAR); month = 0; }
Another answerdkcorps August 17, 2012 at 1:35 PM
import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.*; /** * * @author idealSol3 */ public class dateComp { public static void main(String[] args) { try { String input = "1989-08-18"; SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); Date now = new Date(); Date dob = (Date) format.parse(input); double age = ((now.getTime() - dob.getTime()) / (31556926)) / 1000; System.out.println("age " + age); } catch (ParseException ex) { System.err.println("Error " + ex.getMessage()); } } }
Qوردة الاقصى May 10, 2013 at 5:44 PM
i am need explain this cod please
Post your Comment