Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

1. ## 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

2. ## Re: Date problem

Take a look at the Date, Calendar, and DateFormat classes. The API is your friend.

3. ## 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.

4. ## 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.

5. ## Re: Date problem

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.

6. ## Re: Date problem

Calendar and Date both have before() and after() methods, as well as compareTo() methods.

7. ## 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.

8. ## 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.