Home Discussion Discuss: Determining the actual age from date of birth in Java

  • 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

  • JohnSmithe853 May 10, 2014 at 1:25 PM

    I really like your writing style, great info, thank you for putting up fbkbaadecaedaddf

Post your Comment



Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Related Articles
Java-Tutorials

Determining the actual age from date of birth in Java
Determining the actual age from date of birth in Java... your complete date of birth (year, month and day of month) and gives you the exact... any invalid year or month or day for your date of birth.   Here 

JavaScript Questions

JavaScript calculate age from date of birth
JavaScript calculate age from date of birth  How to calculate age from date of birth?   <html> <head> <script type="text...;body> Date of Birth(dd/mm/yyyy): <input type="text" name="dob" id="dob 

General

Calculate Age using current date and date of birth
Calculate Age using current date and date of birth In this section you... prompted the user to enter current date and date of birth in a specified format. Now... : Enter your Date of Birth in format MM-dd-yyyy 8-28-1989 

JSP Servlet Questions

How automatically calculate age based on date of birth and current date using jsp and servlet?
How automatically calculate age based on date of birth and current date using jsp and servlet?  when user enters the Date of birth in one textbox then automatically age will be display on another textbox. event:Onlick Event 

Java Beginners

age calculator
age calculator  sir , actually m working on a project where i want the user to enter his date of birth in a jtextfield .and on doing so his age should b displayed in the next textfield automatically....plz help 

WebSevices Questions

compare age with dob - WebSevices
* from tableName where date like '2008-12-20'; Thanks... sandeep kumar...compare age with dob  Thanks for your quick reply. but I need exact... as Age like(18). but we dont have specified age field for fetching. instead of age 

Date Calendar

how can you calculate you your age in daies??
in java! can any one help me to write programm to calculate age in daies... of your date of birth: "); int day=input.nextInt(); System.out.print("Enter month(in number) of your date of birth: "); int month 

JSP Servlet Questions

age calculation
age calculation  pls send me the age calculation validation coding in java script. i'm waiting for ur reply 

Java Beginners

Replacing a template with actual values
Replacing a template with actual values  I have a template Hello $$personename$$, You are eligible for this $$planname$$ from today onwards. CONDITIONAL_TEXT_1 Regards,$$managerName$$ I wanna repalce this place 

Swing AWT

Java swing
Java swing  Write a java swing program to calculate the age from given date of birth 

General

Java Date Example
Java Date Examples       Getting astrological sign according to birth date... first provide the birth date. C:\DateExample>javac 

MySQL

Mysql Date Manipulation
and birth from the table :- The Query below find out the field difference in age... table Person(name VARCHAR(20),birth DATE,death DATE); Output...)) -> as age from person -> where death is not null; Output 

JDBC Questions

Subtraction of a date from Sql and other from java.
Subtraction of a date from Sql and other from java.  I want to subtract a date that i retrieve from mysql database with the system date that i retrieve from java. I want the difference between the two date in the integer form 

SQL Question

sql query to get name,age,phone,address from tables t1,t2,t3,t4,t5 ..
sql query to get name,age,phone,address from tables t1,t2,t3,t4,t5 ..   this is for single table : query ="SELECT name , age , pwd , phone... by name"; i want for 12 tables : i want to fetch name age pwd phone address from 12 

MySQL

Mysql Date To Java Date
; Mysql Date To Java Date retrieve the records from Mysql and display the date in java. Understand with Example The Tutorial illustrate an example from... 'mysqldatetable' in java. This Example display date from mysql in java: select 

Date Calendar

Date format - Date Calendar
Date format  Hi All, I am getting a date from database as a format 2010-15-07.I need to change it to 07/15/2010 and display it on my date field.Every time i need to check the format of the date field and make sure the correct 

Java Code Examples

Java subtract days from date
Java subtract days from date In this tutorial, you will learn how to subtract days from date. Using the Calendar class, you can add and subtract date...(-). This will reduce date from date. Here, we are going to subtract few days from 

Java Code Examples

Java subtract months from date
Java subtract months from date In this tutorial, you will learn how to subtract months from date. Using the Calendar class, you can add and subtract date...(-). This will reduce date from date. Here, we are going to subtract few months 

Java Code Examples

Java subtract years from date
Java subtract years from date In this tutorial, you will learn how to subtract years from date. Using the Calendar class, you can add and subtract date...(-). This will reduce date from date. Here, we are going to subtract few years from 

Java Beginners

date
date  i want difference between the date entered in a jtextfield and the system date in java 

Date Calendar

date
date  can u tell me how to calculate difference between a user provided date and the system date in java 

Date Calendar

date
date  can u tell me how to calculate difference between a user provided date and the system date in java 

JSP Servlet Questions

how to generate the pdf file with scroolbar from jsp age - JSP-Servlet
how to generate the pdf file with scroolbar from jsp age  How to generate the pdf file with scroolbar from jsp.i am not able to see the all the columns in pdf file now .it is very urgent for me plz help 

JDBC Questions

java [ cannot retrive date from sql ] why??
java [ cannot retrive date from sql ] why??   import..._date, d,m,y; public void date_mdt()throws ParseException{ String date...-yyyy"); Date da=sdf.parse(getdate); String format=sdf.format(da); try 

Java Beginners

how to get date format in string from date object containing real date in java
how to get date format in string from date object containing real date in java  i want to get data string format from date object where date object containing real date. date frequently vary in from of only month & year 

Java Beginners

how to get date format in string from date object containing real date in java
how to get date format in string from date object containing real date in java  i want to get data string format from date object where date object containing real date. date frequently vary in from of only month & year 

Java Code Examples

Retrieve date from MYSQL database
Retrieve date from MYSQL database In this tutorial, you will learn how to retrieve date from database. Storing and Retrieving dates from the database... of birth of persons from the database table. For this, we have used the TimeStamp 

Java Beginners

Date prorblem
= tokens[8]; .... (the date from source into local java filed...Date prorblem  HI all, i have date comming from one text file into string filed in java code. once the date is available in filed ( of type String 

General

Java Get Month from Date
Java Get Month from Date     ... month from the current date using Java Application Language.  You can see... java.util package in the program code.   Java code to Get Month from Date 

Swing AWT

Swing Program
Swing Program  Write a java swing program to calculate the age from given date of birth 

DMCA.com