• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

how to use date in java

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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..
 
Ranch Hand
Posts: 3178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
Ranch Hand
Posts: 169
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
incandescent light gives off an efficient form of heat. You must be THIS smart to ride this ride. Tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic