What's new

Closed Help src code

Status
Not open for further replies.

E-man001

Addict
Joined
Jun 20, 2016
Posts
127
Reaction
15
Points
89
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:)
 
Last edited by a moderator:
/*
* 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 ... :)
 
Last edited by a moderator:
/*
* 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 :):)
 
Last edited by a moderator:
Status
Not open for further replies.

Similar threads

Back
Top