I need to produce two methods for a class. The method name and details are given but i dont know how to create the code for them to work:

1st:
public int daysElapsed (int day1, int month1, int year1, int day2, int month2, int year2)
This method returns the number of days between any 2 dates.

2nd:
public int ageDate (int dayBirth, int monthBirth, int yearBirth)
This method must return a person’s age in years as at today’s date.

If someone could help me with the code needed, i would be most grateful. If i need to provide any for info please let me know.Thanks.

Recommended Answers

All 4 Replies

You'll need to take a look at the GregorianCalender class.
I don't know much about it, but I do know that you'll have to do some calculating with milliseconds.

I have another method which works out the julian date in the class, i need to use this method to help with the days elapsed method. Any ideas? The julianDate method is:

public int julianDate(int day, int month, int year) {
//The date as in Julian calendar
int julian = 0;
for (int i = 1; i < month; i++) {
switch (i) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12: //months with 31 days
julian = julian + 31;
break;
case 4:
case 6:
case 9:
case 11: //months with 30 days
julian = julian + 30;
break;
case 2:
julian = julian + 28;
if (leapYear(year) == true) julian++;
break;
} //end of switch
} //end of for
julian = julian + day;
return julian;
}

Here is a method I just wrote that calculates the elapsed days. I used the GregorianCalendar. You can calculate milliseconds, months, years the same way.

import java.util.*;

public class GetDays
{
   public int getElapsed(GregorianCalendar g1, GregorianCalendar g2)
   {
	int elapsedDays = 0;
	GregorianCalendar gc1 = null;
	GregorianCalendar gc2 = null;
	if (g2.after(g1))
	{
		gc2 = (GregorianCalendar)g2.clone();
		gc1 = (GregorianCalendar)g1.clone();
	}
	

	while(gc1.before(gc2))
	{
		gc1.add(Calendar.DATE,1);
		elapsedDays++;
	}
	
	return elapsedDays;
   }

   public static void main(String[] args)
   {
	GetDays gd = new GetDays();
	System.out.println("Days past:  " + 
	gd.getElapsed(new GregorianCalendar(2003,Calendar.OCTOBER,1),
		      new GregorianCalendar(2005,Calendar.NOVEMBER,2)));
   }
}

I need to use the JulianMethod in there somehow? any ideas

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.