This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes how to use date in java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "how to use date in java" Watch "how to use date in java" New topic
Author

how to use date in java

hrin kuek
Greenhorn

Joined: Mar 24, 2005
Posts: 9
Hi,

If I want a user to input a date alone, not with time, how can I do it? Eg. A customer calls to make a reservation at a restaurant, the user has to key in a date that the customer wants to reserve the place for.

I can't use int cos validating data will be very troublesome right? Is it possible to show me an example? Furthermore, I need to store the date attribute in an object reservation and to later on implement a search function based on the date.

Would really appreciate an example. Thanks so much..
Ko Ko Naing
Ranch Hand

Joined: Jun 08, 2002
Posts: 3178
Originally posted by hrin kuek:

I can't use int cos validating data will be very troublesome right? Is it possible to show me an example? Furthermore, I need to store the date attribute in an object reservation and to later on implement a search function based on the date.

Right, you should not use long or int to store milliseconds of a Date...

Just use a Date attribute in your bean... You might want to use Java's integrated regex to validate the format of the data that the user inserted into a textfield...

For more information about regex package, u might want to have a look at this tutorial.

Just my 2 cents...


Co-author of SCMAD Exam Guide, Author of JMADPlus
SCJP1.2, CCNA, SCWCD1.4, SCBCD1.3, SCMAD1.0, SCJA1.0, SCJP6.0
Jeff Grant
Ranch Hand

Joined: Dec 19, 2001
Posts: 169
Java is pretty smart.. so just create a new java.util.Date(year, month, day) with the user entered values.

To check the validation of the date entered, do a comparison of the year, month, and day entered with the one that the variable now holds.

For example:

Date Invalid would be displayed at the prompt because the date 2/29/2003 is not valid as 2003 is not a leap year. date would actually hold 3/1/2003. This is very handy for doing math in calendar programs.

Bear in mind, however, that java.util.Date has been deprecated as of JDK 1.1 and replaced with Java.util.Calendar. It has basically the same functions and can be replaced just as easily in my code above.

p.s. I did not compile this code snippet above so I'm sorry if it has an error.
[ March 25, 2005: Message edited by: Jeff Grant ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to use date in java
 
Similar Threads
urlyBird search requirement
Class Modeling
Question on EJB 3 transaction
Sequence Diagram
How do I do a partial word search?