File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to use date in java

 
hrin kuek
Greenhorn
Posts: 9
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3178
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Jeff Grant
Ranch Hand
Posts: 169
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic