To me this is a hard question. I hope I'm posting this in the right area if not I'm sorry.
In the future, I want to be a java programmer so I'm creating an application. At this point, I'm inserting dates into a database as for example 02/19/2005 and displaying it as February 19, 2005 using dateformat. My problem, that I can't figure out, is how can I restrict entering past dates for example 01/01/2005 and so on? I've read about using the Calendar object but I need an example to get it going. I've tried
but not sure where to go from here. A tutorial or code samples would be great. Thank you in advance.
. [ February 19, 2005: Message edited by: Sue Miller ]
I've been going round and round until I'm about to fall down. I'm confused big time. Here is my latest code and all I want is to compare the present date with the past date. If the date is in the past send it to an error page. I'm just not skilled enough to figure out how to do this. I've tried you code but it doesn't work from me. I can't get it to go to else no matter what date I type in. If I can get that then I'll be able to make it go to an error page. Can Joyce or anybody else see why this isn't happening?
Given the code you posted, I don't see that it will ever go into the else branch either. When will the current date/time ever be before sometime during the day of Jan 1st, 2005?
By the way, if you want to get rid of the "sometime during the day of" part, you need to clear() "test" before you set the year, month, and day as the other time fields are kept.Also, you may want to extract the time value in milliseconds and use that. Then there's no need to create a new Calendar each time.
Originally posted by David Harkness: [QB]Given the code you posted, I don't see that it will ever go into the else branch either. When will the current date/time ever be before sometime during the day of Jan 1st, 2005?
Answering your question. It will if I type in a date that is before Jan 1st, 2005. See I have a form field and I want to restrict dates that are in the past. So if I type December 31, 2004 I want to redirect it to an error page and say "Date is in the past you can't enter it. Or say I have it setup and Today is February 22, 2005, I want to restrict somebody typing February 21, 2005 or anything in the past. I want the form to realize what the current date is and if somebody types a date in the past direct it to an error page. If the date is in the future then direct it to the OK page etc.
. [ February 21, 2005: Message edited by: Sue Miller ]
Okay, that's what I understood but I got thrown off by the comments in your code. Are you able to understand the code I posted? It's very similar to what you had.
One thing to do when you aren't getting the results you expect is to print out variables to the screen. Print out both dates that you are comparing. The built-in date formatter should be sufficient for you to make sure you have the correct values.If the dates aren't what you expect, that lets you know there's a problem somewhere in creating the dates. For one thing, it would have shown you that you weren't clearing the time of day when setting the date entered by the user.
If you just want dates (no times), make sure you set those fields to zero or clear the Calendar before setting the date.
I've read and read but still not sure how to construct what I want to do.
I think I understand your code. In fact as soon as I'm finished here I'm going to get to work on my project. I can do without the lines below since I'm not trying to get the times but instead the current date and the date the user types in, correct?
I gues on this line of code -
I can instead have something like
which will be the value the date will carry over from the submit page. Well I'll use the get and set I would think. Because I have to get it and then set it.
Also this line
Shouldn't it be like this instead?
Thanks for your help and for being patient with a new java person. I hope I start understanding this stuff better, very soon.
. [ February 22, 2005: Message edited by: Sue Miller ]
Okay, so you're taking input from the user (either a String or individual integers, which is it?) and you want to make sure the date is not in the past. Is this correct? That simplifies things a little.DateFormat.parse() returns a Date, so there's no need to cast it. Also, see the comments: "test" holds the value entered by the user in "YYYY/MM/DD" format. If you are getting this data in some other format, by all means post how you get it, for example as three numbers (int).
Then change this lineto thisLet me know if you need help handling the case where they type in an invalid format or bogus date, like "15/83/2005." DateFormat.parse() will throw an exception in both cases.