Hey guys I'm working on a program that asks the user to enter a month and year and then prints that month's calendar page on the screen. However, to do this I created a daysBetween() method that accepts 2 dates and finds the number of days between them. So basically I need some advice for subtracting 2 dates and finding the days between them... I don't want code but just general ideas for algorithms or approaches I could use. Because it's proving to be much more difficult to implement than I previously thought.

Thanks so much!
~Kvass

Create Calendar objects, get the epoch times for each, subtract and divide.

epoch times?

Please be more specific. I don't need code, but I need more thorough explanation.

epoch times

Ask Google or read the API doc for Date or Calendar.

Which part don't you understand?
Use SimpleDateFormat to get a Date object for the two dates.
Get the epoch times for each date.
Subtract the times
Convert the time difference to days.

Ok -- but I was kind of hoping to solve this problem on my own without the assistance of other objects (aside from Scanner for user input). Because I would rather understand the mathematical or algorithmic approach to this problem rather than just use an object that someone else made for me.

Go ahead and write as much code as you want.
I've reinvented a few wheels in my time. You learn sometimes. Other times, you just waste time.

Ok -- so can some1 help give me a pointer on reinventing this wheel?

I need some advice for subtracting 2 dates and finding the days between them... I don't want code but just general ideas for an algorithm I could use. Because it's proving to be much more difficult to implement than I previously thought. And I don't want to use the Calendar and Date classes predefined in the Java API.

Starting at older date, add one day to it and see if it is the end date.
Define a class that defines a date as day, month, year.
Add methods to it:
to add a day and
to compare against another date

Or:
Write a method to convert a date (int yyyy, int mm, int dd) to the number of days after Jan 1st 1900 (or some other convenient earliest date) - something along the lines of

int daysAfterJan1st1900(int yyyy, int mm, int dd) {
  return (yyyy - 1900)*365 + (something for leap years) + (number of days in each month up to mm) + dd;
}

Once you have written this, then you can trivially convert both your dates to days after 1/1/1900 and subtract them. You can also get day of the week by just calculating %7 on that.
(This is how date libraries usually work.)

Is there any problem for 1900 in java calender.
I am trying to add 1900 but it gives error

No problem (except that it's not a leap year, even tho it's divisible by 4).

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.