Share on Google+Share on Google+

sh
how can you calculate you your age in daies??
5 Answer(s)      6 years and 4 months ago
Posted in : Date-Calendar

**hi,

I am beginner in java! can any one help me to write programm to calculate age in daies???**

Ads

View Answers

December 9, 2010 at 1:33 PM


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



December 9, 2010 at 11:23 PM


thanks alot but how can we cheach if user enter wrong input??

all the best,,,



December 9, 2010 at 11:29 PM


why you use (24 * 60 * 60 * 1000) each number is for what??



December 11, 2010 at 3:59 PM


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



December 14, 2010 at 2:56 PM


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");

}

}










Related Tutorials/Questions & Answers:
Advertisements
Tutorials   
Java Spring Hibernate Struts Training is this programm efficient to find the day of the given date . can it be more normalized ? date program What is License Terms for using your code current date set date date Cannot convert 1/23/13 1:18 PM of type class java.util.Date to class java.sql.Timestamp Dyanmically Adding Rows with Date Picker Dyanmically Adding Rows Calender in JSP Calender in JSP richfaces jsf how to convert the entire column field of table to dd/mm/yyyy format in microsoft sql server? Database Countdown timer to show a link Find whether a date will be in the current quarter or the next quarter prudhvi code Buy Flowers Online i want to create dynamic calendar in java i want to create dynamic calendar in java date Addintion in year fields of sql date in a java page storing date from html form to oracle 10g using servlet code can u help me to get a calendar with color codes on days ??? html retrieve value from db in text box + calendar implementation. How to add two calendars on the same html page validationrule for date as format yyyy-mm-dd in drools java program program jquery date picker database mysql want the code for calender in php+htmal page Reading the Date and time values from excel sheet Baloon Popup in GWT project calender event Only Display Date date and time in awt(java) time get date picker values to a textbox conept - Date Calendar How to display current date in textbox - Date Calendar this is my code java - Date Calendar ask how function jCalender - Date Calendar Set Date limit for dojo date picker dynamically - Date Calendar

Ads

 
Advertisement null

Ads