Reputation: 275
I am using 2 GWT datepickers and a JAVA date object. I want to check if the current date (from the now() function) is in between the dates selected in the datepickers. I have 2 questions:
How do I ensure that the date picked in the second date picker is atleast the same or later than the one picked in the first one?
I want to set the same date in both date pickers and check my current date against it. I tried to do this by setting the time on my second date to 11:59:59 PM, but it says that the Date.setHours and setMinutes methods are depricated.
Any ideas? Appreciate any help.
p.s. Please dont tell me to use Calendar or any other date function. My constraint is that I need to use java.util.Date.
Upvotes: 2
Views: 3624
Reputation: 18331
Despite what your IDE has to say about it, using the old java.util.Date
methods aren't so bad. In regular Java code, yes, use the Calendar
class instead, but for historical reasons, think twice about not using those 'deprecated' methods.
Once upon a time, Java was the coolest kid around - weird, right? Web browsers didn't have such a language, and someone said "Hey, lets give them a scripting language, but call it something, I dunno, cool!" Thus, we were given JavaScript - more of a Lisp-like language than Java, but never mind that.
Some APIs were borrowed, such as the Date object. Then, Java decided that it could deal with dates and times and timezones better than that, deprecated those functions such as setHours
, and moved on to the bright (and confusing) future of Calendar, JodaTime, etc that we know and love today.
The browsers had JavaScript though, and were finding their own way, and kept on with those methods. There are JavaScript libraries that can help deal with dates, but within the browser runtimes, the Date object still looks like the old java.util.Date
.
So when in GWT, write in Java where you like, but always remember that you are in a web browser - the underlying language just so happens to have native functionality that uses those deprecated methods - and they aren't going anywhere. Likewise, even though Object.wait()
and notify()
are legal, you can't use them, nor will your finalize()
ever be invoked - these are just things you need to keep in mind when using Java to organize and maintain your library or application, and compiling to run in web browsers using JavaScript.
Upvotes: 6
Reputation: 41089
To compare dates simply use
date2.getTime() >= date1.getTime();
Note that you can't compare dates directly - Java allows it but there are issues in JavaScript.
To set the same dates, get date from one date picker, and set it to another.
Also note there is a CalendarUtil class in GWT (com.google.gwt.user.datepicker.client.CalendarUtil) which has limited functionality, but it is still very useful in many situations.
You can use deprecated methods with Date objects - no one plans to remove them any time soon. I try to avoid them by using milliseconds when possible, but it's so much easier, for example, to check a day of the week using a deprecated method.
Upvotes: 4