/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package computeage;
/**
*
* @author JustFriends
*/
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
DateTime dt = new DateTime("1999-09-12"); // dito mo po ilagay ang age ng tao
try {
System.out.println(getAge(dt, DateTime.now()));
} catch (Exception e) {
System.out.println(e.getMessage());
System.out.println("Kindly check your entered value.");
}
}
public static String getAge(DateTime birthday, DateTime now){
if (now.compareTo(birthday) < 0)
{
System.out.println("Please recheck age.");
}
Interval interval = new Interval(birthday,now);
long diffDays = (long)interval.toDuration().getStandardDays();
if (diffDays > 7)//year, month and week
{
int age = now.getYear() - birthday.getYear();
if (birthday.isAfter(now.plusYears(-age))) age--;
if (age > 0)
{
return age + (age > 1 ? " yrs" : " yr");
}
else
{// month and week
DateTime d = birthday;
int diffMonth = 1;
while (d.plusMonths(diffMonth).isBefore(now) && d.plusMonths(diffMonth).isEqual(now))
{
diffMonth++;
}
age = diffMonth - 1;
if (age == 1 && d.isAfter(now)) age--;
if (age > 0)
{
return age + (age > 1 ? " mts" : " m");
}
else
{
age = (int) diffDays / 7;
return age + (age > 1 ? " wks" : " wk");
}
}
}
else if (diffDays > 0)
{
long age = diffDays;
return age + (age > 1 ? " dys" : " dy");
}
else
{
return "Cannot calculate age";
}
}
}
PS: Gamit po tayo ng Jodatime library para po mas madali ang computation ... free library po siya (You do not have permission to view the full content of this post.Log in or register now.)
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package computeage;
/**
*
* @author JustFriends
*/
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
DateTime dt = new DateTime("1999-09-12"); // dito mo po ilagay ang age ng tao
try {
System.out.println(getAge(dt, DateTime.now()));
} catch (Exception e) {
System.out.println(e.getMessage());
System.out.println("Kindly check your entered value.");
}
}
public static String getAge(DateTime birthday, DateTime now){
if (now.compareTo(birthday) < 0)
{
System.out.println("Please recheck age.");
}
Interval interval = new Interval(birthday,now);
long diffDays = (long)interval.toDuration().getStandardDays();
if (diffDays > 7)//year, month and week
{
int age = now.getYear() - birthday.getYear();
if (birthday.isAfter(now.plusYears(-age))) age--;
if (age > 0)
{
return age + (age > 1 ? " yrs" : " yr");
}
else
{// month and week
DateTime d = birthday;
int diffMonth = 1;
while (d.plusMonths(diffMonth).isBefore(now) && d.plusMonths(diffMonth).isEqual(now))
{
diffMonth++;
}
age = diffMonth - 1;
if (age == 1 && d.isAfter(now)) age--;
if (age > 0)
{
return age + (age > 1 ? " mts" : " m");
}
else
{
age = (int) diffDays / 7;
return age + (age > 1 ? " wks" : " wk");
}
}
}
else if (diffDays > 0)
{
long age = diffDays;
return age + (age > 1 ? " dys" : " dy");
}
else
{
return "Cannot calculate age";
}
}
}
PS: Gamit po tayo ng Jodatime library para po mas madali ang computation ... free library po siya (You do not have permission to view the full content of this post.Log in or register now.)