how can you calculate you your age in daies??

how can you calculate you your age in daies??

**hi,

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

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:
how can you calculate you your age in daies??
Calculate Age using current date and date of birth
Advertisements
What age should you start a career?
Calculate an average Age of my Class?
JavaScript calculate age from date of birth
Determining the actual age from date of birth in Java
How automatically calculate age based on date of birth and current date using jsp and servlet?
how can you say java is secure
How can you tell a fake data scientist?
How many qusetions can you ask?
Can you tell me how it worked?
can you explain ?
Please can you help
Can you correct this codes?
You handle your own transaction
How can you add elements of two arrayLists in java?
How can you handle form in Spring 3.2 MVC?
When you look back on the position you held last, do you think you have done your best in it?
Want To Know What You Can Do Expect From Your New iPad? Look No Further?
How you can escape a String for HTML using the StringEscapeUtils
Can you help me with applet?
Can you get a job with Dataquest?
Can you make AI in Python?
Can you build AI with Python?
How would you honestly evaluate the strengths and weaknesses of your previous/current company/boss/team?
How will you configure Hibernate?
What would you rate as your greatest weaknesses?
how can i calculate loan - Java Interview Questions
Maven dependency for com.clever-age - play2-elasticsearch version 1.1.0 is released. Learn to use play2-elasticsearch version 1.1.0 in Maven based Java projects
Maven dependency for com.clever-age - play2-elasticsearch version 0.8.1 is released. Learn to use play2-elasticsearch version 0.8.1 in Maven based Java projects
Maven dependency for com.clever-age - play2-elasticsearch version 0.8.2 is released. Learn to use play2-elasticsearch version 0.8.2 in Maven based Java projects
What Systems you can migrate to Cloud?
How to calculate the average in Hibernate?
ModuleNotFoundError: No module named 'can-you-guess'
Can you create an object of an abstract class?
Can you instantiate the Math class? - Java Beginners
Can you provide me Hibernate configuration tutorial?
Can you become a data scientist without a degree?
Can you work part time as a data scientist?
Can you work as a data scientist from home?
Can you teach yourself machine learning?
Can you learn python in 2 days?
Hey neighbor, can you lend some Hammers?
Hey neighbor, can you lend some Hammers?
can you check below code if any erors
How to calculate average of array in Java?
Make Profit with Outsourcing,Profit from Outsourcing,How You Can Profit from Outsourcing
How do you define a constant?
How do you sum a dictionary
How are you learning AI and ML?

Ads