🔒 Closed Help src code

Status
Not open for further replies.

E-man001

Journeyman
Mga masters baka naman po mayron kayo src code pano makuha ang total age pag inimput ang month, year at day ng birth day mo.
Using JCreator. TIA:)
 
/*
* 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;

import org.joda.time.DateTime;
import org.joda.time.Interval;

/**
*
* @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.)

Sana po makatulong kahit papaano ... :)
 
/*
* 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;

import org.joda.time.DateTime;
import org.joda.time.Interval;

/**
*
* @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.)

Sana po makatulong kahit papaano ... :)
Salamat po :):)
 
Status
Not open for further replies.

About this Thread

  • 5
    Replies
  • 745
    Views
  • 3
    Participants
Last reply from:
JustFriends

Trending Topics

Online now

Members online
1,122
Guests online
1,012
Total visitors
2,134

Forum statistics

Threads
2,274,680
Posts
28,957,598
Members
1,233,400
Latest member
lelouch05
Back
Top