// first import Calendar
import java.util.Calendar;
public class HowYoungAreYou{
public static void main(String[]args){
//set current time and date,ngayon!kung anong date sa PC
Calendar now=Calendar.getInstance();
//set yung birthdate mo
Calendar before=Calendar.getInstance();
//halimbawa birthdate mo ay July 25,2000
before.set(2000,6,25);
/* month is an array,
and array start with 0-January,1-Febuary
therefore 2000,6,25 is July 25 2000*/
//convert time in milliseconds
//declare nowmillis and beforemillis in "long" integer
long nowmillis=now.getTimeInMillis();
long beforemillis=before.getTimeInMillis();
//find the difference of date in milliseconds
//declare also timemillis as "long" integer
long timemillis=nowmillis-beforemillis;
//compute/convert millisecond in to days
long days=timemillis/(24 * 60 * 60 * 1000);
//1000ms in a second,60 in a minute,60minute in an hour and 24hours in a day
//then days to years,365days in a year, declare in double for decimal decimal
double year=(double) (days/365d);
System.out.println("You are"+year+"year(s) old right now!);
}
}/Done
Output:
You are 11.70958904109589 year(s) old right now!
More examples by request
More lessons to come.
SHARE and LEARN!
No comments:
Post a Comment