Int days=daysBetween(cal1.getTime(),cal2. Yy=Integer.parseInt(JOptionPane.showInputDialog(“YY: “)) Ĭal2.set(yy, mm, dd) // set date of birth Mm=Integer.parseInt(JOptionPane.showInputDialog(“MM: “)) (“Enter your date of birth(dd mm year): “) ĭd=Integer.parseInt(JOptionPane.showInputDialog(“DD: “)) Public static void main(String str) throws Exception else if (months = 0 & now.get(Calendar.DATE) birthday.get(Calendar.DATE))ĭays = now.get(Calendar.DATE) – birthday.get(Calendar.DATE) Įlse if (now.get(Calendar.DATE) < birthday.get(Calendar.DATE))ĭays = now.getActualMaximum(Calendar.DAY_OF_MONTH) – birthday.get(Calendar.DAY_OF_MONTH) + today Let me know your thoughts on the above solutions. In above examples, we learned to write a program to calculate the age of a person in java. SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy") Public static void main(String args) throws ParseException Int today = now.get(Calendar.DAY_OF_MONTH) ĭays = now.getActualMaximum(Calendar.DAY_OF_MONTH) - birthDay.get(Calendar.DAY_OF_MONTH) + today
can use Textpad or notepad++ or you can download an ide called bluej which is ideal. if month difference is in negative then reduce years by oneĭays = now.get(Calendar.DATE) - birthDay.get(Calendar.DATE) Įlse if (now.get(Calendar.DATE) < birthDay.get(Calendar.DATE)) Lets assume that your Java application is, sorry by the redundancy. Int birthMonth = birthDay.get(Calendar.MONTH) + 1 Int currMonth = now.get(Calendar.MONTH) + 1
Years = now.get(Calendar.YEAR) - birthDay.get(Calendar.YEAR)
Long currentTime = System.currentTimeMillis() Private static Age calculateAge(Date birthDate)Ĭalendar birthDay = Calendar.getInstance() ītTimeInMillis(birthDate.getTime()) Second class is AgeCalculator itself which has a private method which accept a person’s date of birth and return the Age instance having person’s age information. Return years + " Years, " + months + " Months, " + days + " Days" Public Age(int days, int months, int years)
We can add parameter validation as well so that it does not accept negative values. I have made the default constructor private and provided a constructor which accepts days, months, years. I have made some effort by adding Age class to make this solution look professional. The low performance (not measured accurately) can be also a downside.
Though it works fine but code is not very readable. I will not recommend this method for production-class applications. This solution is very basic and should be used for only learning the logic of calculation. The following example program in Java calculates BMI value based on inputs in. The only difference between the above two examples is method “ between() “ which is not available in Period class in Joda library, rather dates are passed to the constructor. Example calculation for weight 90kg, height 1.5m. Period period = new Period(birthdate, now, PeriodType.yearMonthDay()) LocalDate now = new LocalDate() //Today's date Find simple interest using formula SI (principle time rate) / 100. Input principle amount in some variable say principle. Step by step descriptive logic to calculate simple interest. LocalDate birthdate = new LocalDate (1970, 1, 20) //Birth date Where, P is the principle amount T is the time and R is the rate. So this solution is also very similar to Java 8 solution. Joda also has Period class that is very much similar to Java 8 Period class. We know that the new Java 8 date time API is inspired by the Joda library. Period p = Period.between(birthday, today) LocalDate birthday = LocalDate.of(1980, Month.JANUARY, 1) // Birth date LocalDate today = LocalDate.now() // Today's date is 10th Jan 2022 Note that birthday is inclusive, while today date is exclusive in the calculated period. Once the Period is obtained, we can get the difference between both dates in desired metrics. We are using Period to store the difference between today’s date and the person’s date of birth. The program uses Period class to store the differences between two LocalDate instances. Given below is a Java 8 program to calculate the age of a person from the date of birth. In Java 8, Period class is used to define an amount of elapsed time with date-based values (years, months, days). Reverse Polish Notation Stack Example Implementing StepsĬopy the Stack code on Java Stack Implementation page.Ĭopy the following code and save as Postfi圎valuation.javaĬode Example /****************************************************************************** Java Program to create a Calculator - To create a calculator with Java Swings, try the following code Exampleimport import.
Result of applying the operator to those operands. Number of operands and push onto the stack the On encountering an operator, pop the requisite It's much easier for us to calculate Postfix Expression by using stack. There are no precedence rules, no parentheses needed. Postfix is a expression of Arithmetic Expressions in which the operands are placed before their operators.