**hi,
I am beginner in java! can any one help me to write programm to calculate age in daies???**
Hi Friend,
Try the following code:
import java.util.*; public class AgeInDays{ public static void main(String[] args) throws Exception{ Scanner input=new Scanner(System.in); System.out.print("Enter day of your date of birth: "); int day=input.nextInt(); System.out.print("Enter month(in number) of your date of birth: "); int month=input.nextInt(); System.out.print("Enter year of your date of birth: "); int year=input.nextInt(); System.out.println(); Calendar cal1 = Calendar.getInstance(); Calendar cal2 = Calendar.getInstance(); cal1.set(year, month, day); cal2.set(2010, 12, 9); long milis1 = cal1.getTimeInMillis(); long milis2 = cal2.getTimeInMillis(); long diff = milis2 - milis1; long diffDays = diff / (24 * 60 * 60 * 1000); System.out.println("Your Age is: " + diffDays + " days."); } }
Hope that the above code will be helpful for you.
Thanks
thanks alot but how can we cheach if user enter wrong input??
all the best,,,
why you use (24 * 60 * 60 * 1000) each number is for what??
Hi Friend,
Try this:
import java.util.*; public class AgeInDays{ public static void main(String[] args) throws Exception{ Scanner input=new Scanner(System.in); System.out.print("Enter day of your date of birth: "); int day=input.nextInt(); while(day>31){ if(day<1||day>31){ System.out.println("Re-Enter day. It should not exceeds 31!"); day=input.nextInt(); } } System.out.print("Enter month(in number) of your date of birth: "); int month=input.nextInt(); while(month>12){ if(month<1||month>12){ System.out.println("Re-Enter month. It should not exceeds 12!"); month=input.nextInt(); } } System.out.print("Enter year of your date of birth: "); int year=input.nextInt(); System.out.println(); Calendar cal1 = Calendar.getInstance(); Calendar cal2 = Calendar.getInstance(); cal1.set(year, month, day); cal2.set(2010, 12, 9); long milis1 = cal1.getTimeInMillis(); long milis2 = cal2.getTimeInMillis(); long diff = milis2 - milis1; long diffDays = diff / (24 * 60 * 60 * 1000); System.out.println("Your Age is: " + diffDays + " days."); } }
The 24 * 60 * 60 * 1000 helps in converting the milliseconds in a number of days.
Thanks
hi; i solve it with this code ... i hope it will help others
import java.util.*; public class AgeInDays { public static void main(String[] args) throws Exception { Calendar cd= Calendar.getInstance();
Scanner input=new Scanner(System.in);
System.out.print("Enter year of your date of birth: "); int year=input.nextInt(); if(year > cd.get(Calendar.YEAR)) {
System.out.print("Invalid date of birth."); System.exit(0); } System.out.print("Enter month(in number) of your date of birth: "); int month=input.nextInt(); if(month < 1 || month > 12) { System.out.print("Please enter monthe between 1 to 12."); System.exit(0); } System.out.print("Enter day of your date of birth: "); int day=input.nextInt(); if( month == 2) { System.out.print("Please enter day between 1 to 29."); } else if(month == 1 || month == 3 || month == 5 || month == 7 ||month == 8 || month == 10 || month == 12) { if(day > 31 || day < 0) { System.out.print("Please enter day between 1 to 31."); System.exit(0); } } else if(month == 4 || month == 6 || month == 9 || month == 11) { if(day > 30 || day < 0) { System.out.print("Please enter day between 1 to 31."); System.exit(0); } } else{ if(new GregorianCalendar().isLeapYear(year)) { if(day < 0 || day > 30) { System.out.print("Please enter day between 1 to 29."); System.exit(0); } } else if(day < 0 || day > 29){ System.out.print("Please enter day between 1 to 28."); System.exit(0); } } System.out.println(); int difmonth=cd.get(Calendar.MONTH)-month; int daymonth=difmonth*30; int difday=cd.get(Calendar.DAY_OF_MONTH)-day; int difyear=cd.get(Calendar.YEAR)-year; int age= (365*difyear)+difday+daymonth ; System.out.println("Your Age is: " +age + " day"); }
}
Ads