Hello,
How can I retrieve the system date in java and calculate the age of a person knowing his date of birth? If possible, give me the full syntax. Any idea regarding this topic will be highly appreciated. Thank you in advance
Hello,
How can I retrieve the system date in java and calculate the age of a person knowing his date of birth? If possible, give me the full syntax. Any idea regarding this topic will be highly appreciated. Thank you in advance
Hello,
For the current date in milliseconds:
to the date of birth:long test = System.currentTimeMillis();
Then you have to play with the setters, and it retrieves the date in milliseconds as follows:Code:java.util.Calendar Brh = Calendar.getInstance();
Subtraction will do for the rest and I'll leave it to the conversion years.Code:long test1 = birth.getTime();
Hello,
You can extract the years, get (Calendar.YEAR). You will get and the year of birth and the current year, example
In this case the person in 24 years, then uses the method add (Calendar.YEAR, -24), and compares the date, removes one years if 24 years before the person is not born: she was born after October 22.Code:February 13, 1985 -> 1985 October 22, 2009 -> 2009
Hello,
For the above explanation, I have developed a code, please check it out. Hope this will help you.
Code:Public static int getYear(Date of) { Calendar cu = Calendar.getInstance(); Calendar brt = Calendar.getInstance(); brt.setTime(d); int yeardiff = cu.get(Calendar.YEAR) - Birth.get(Calendar.YEAR); cu.add(Calendar.YEAR,-yeardiff); if(brt.after(cu)) { yeardiff = yeardiff -- 1; } return yeardiff; }
Hello,
In the calendar class there are set methods that allow all precisely define what we want:
So you create an object Calendar Using the method static Calendar getInstance ()Then you change that date at its convenience. When you look in the javadoc, which function returns a date: you need a date. You should not even need to read the javadoc for this finding: it is in my program, but still Surprise is getTime (). So after long search in the page cited above we find the function getTime (), so you get the Date object.Code:set(int year, int month, int date)
Hello,
Please see the following code, I hope it will help you
Code:import java.util.Calendar; import java.util.Date; class Test2 { Public static int getYear(Date of) { Calendar cu = Calendar.getInstance(); Calendar brt = Calendar.getInstance(); brt.setTime(d); int yeardiff = cu.get(Calendar.YEAR) - Birth.get(Calendar.YEAR); cu.add(Calendar.YEAR,-yeardiff); if(brt.after(cu)) { yeardiff = yeardiff -- 1; } return yeardiff; } Public static void hand(String[] args) { if(args.length != 3) { System.err.System.out.println("Standard"); System.exit(1); } Calendar ca2 = Calendar.getInstance(); ca2.set(Integer.parseInt(args[0]), Integer.parseInt(args[1])-1, Integer.parseInt(args[2])); Date bday = ca2.getTime(); System.out.System.out.println("The age of the master:"+ getYear(bday)); } }
Bookmarks