# Date problem

• April 21st, 2011, 07:52 AM
java_wannabe
Date problem
Hi everyone,

I am trying to create a program that asks the user for the date and then checks if this date is the last day of the month. However I am having problems doing this. Could anyone suggest where to start? I can get the user to input a day using scanner or JOptionPane but then im not sure where to go from there?

Thanks
• April 21st, 2011, 08:01 AM
KevinWorkman
Re: Date problem
Take a look at the Date, Calendar, and DateFormat classes. The API is your friend.
• April 21st, 2011, 09:02 AM
ShaunB
Re: Date problem
I've written a Date class, I could send you the code with controller with the logic, just have a muck about with that. But days of each month are (from Jan - Dec):

31, 28*, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31

Put that into a matrix and set up an interger for the day and month. So:

IF month==10
THEN LET numberOfDays=31

Just compare your day integer to that. If it's not equal, then return false, else return true. Simple?

Also note that if you're going to use an array or matrix, the computer counts each entry from zero, so to get the value from a matrix, you'll have to say something like:

LET mon=noOfDays[month-1]
IF day==mon
THEN LET endOfMonth=TRUE
ELSE LET endOfMonth=FALSE

Or at least that's something close to pseudo-code. See the logic? If you do, that's half of the problem solved.

Regards,

Shaun.
• April 21st, 2011, 09:49 AM
KevinWorkman
Re: Date problem
No offense intended, but I think that's reinventing the wheel to a certain extent. That's fine if the purpose of the exercise is to learn (like using an array when an ArrayList is more appropriate), but this functionality already exists in the classes I mentioned. Up to you though.
• April 21st, 2011, 11:34 AM
ShaunB
Re: Date problem
Quote:

Originally Posted by KevinWorkman
No offense intended, but I think that's reinventing the wheel to a certain extent. That's fine if the purpose of the exercise is to learn (like using an array when an ArrayList is more appropriate), but this functionality already exists in the classes I mentioned. Up to you though.

The course I'm on requires you to build your own Date class because it presents an interesting challenge.

I'm not great at remembering exact syntax, but isn't there a boolean condition for Date, something like dateVar.isEarlierThan(testDate), which works something like this:

if (!dateVar.isEarlierThan(testDate)) { return false; }
else return true;

Not 100% sure on that one, in fact, I might be thinking of another language altogether.

But, if I'm somewhere close to being correct, as long as your testDate is the last day of the month, it'll work.

Regards,

Shaun.
• April 21st, 2011, 11:39 AM
KevinWorkman
Re: Date problem
Calendar and Date both have before() and after() methods, as well as compareTo() methods.
• April 21st, 2011, 03:36 PM
ShaunB
Re: Date problem
A note about ArrayList types: some earlier Java IDEs don't like this. I know because the College has a lot of really old software installed on their PCs. I don't understand why educational institutions have an aversion to upgrading free software?!? Oh well... Anyway, we run a really old version of NetBeans, and also a slightly newer version of BlueJ. I can't remember which way around it is, but one of them is happy with ArrayLists and the other isn't.

The only reason I'm pointing this out is that some businesses here in the UK also run 'legacy' systems. When I did telesales in 2006ish, a warehouse was using an old 486-DX25 terminal to run and maintain its' database. I was like "it's like being at home: where is the new technology???!!!" I suppose some business thinking is 'why upgrade and pay for staff training etc... if the status quo works'.

So, don't always assume that clients have the latest kit and laters JDKs or whatever.

Regards,

Shaun.
• April 22nd, 2011, 09:25 AM
KevinWorkman
Re: Date problem
I'd bet it's more likely that the older systems don't "like" generics. They are used pretty often with ArrayLists, and have only been around since Java 1.5 (or somewhere around there, I'm too lazy to look it up right now), and ArrayLists have been around since 1.2.