Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • 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
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

dates, strings, and confusion

 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey everyone,

I need some help thinking through how to write a method. I am simulating a program that would take a reservation for a hotel.

I need this method to cycle through a list and return every instance of a particular date.

I have read the Java Docs on SimpleDateFormat and DateFormat and but am still stumped. I don't expect anyone to write this for me ~ I'm just asking for help on how to think this through.

Here is what I have...

...the "String checkIn" is a string containing a date from the class Reservation in the MMM-dd-yy format.



Thanks a million!
 
Ranch Hand
Posts: 689
Scala Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Adam,

welcome to java ranch ....

Pleace code tag when your question contains code.

read this Use Code Tags ..
 
Ranch Hand
Posts: 214
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First please post, what is checkIn (the method parameter)? A string containing a date? If so, which format? Then, does a reservation contain a Date (java.util.Date) or does it contain the date in string form or something else?

If you have a Date in your reservations, you could simply get a string from that by using something like


Hope this helps!
 
Adam Confino
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First, an apology to all for bad formatting.

So the concept is that you would take two strings (one would be user input, one is data in the model\database), convert them to dates using SimpleDateFormat, and then comparing the date objects to each other??

Thanks again, I really do appreciate it.
 
Ranch Hand
Posts: 624
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Adam Confino wrote:
So the concept is that you would take two strings (one would be user input, one is data in the model\database), convert them to dates using SimpleDateFormat, and then comparing the date objects to each other??


That is probably how I would go about it. The key is you need to compare apples to apples. So you either need to compare a Date to a Date or a String to a String. Since you are woking with Dates, it makes sense to use Date objects, not Strings. This gives you all the abilities that the Date class brings to you. If you used Strings, you can't determine that 2009-09-04 is equal to 04-SEP-2009. But if you convert those Strings to Date objects, you can now compare them. (You would need two different SimpeDateFormat objects to convert the two different Strings to dates.)

One thing to keep in mind is that a Date object is really a DateAndTime object. In other words, it not only stores the date, but the time to millisecond precision. So if I do this:


Even though they will have the same date, the objects are not equal since they will have different times. The key point there is that if you truly only care about the Date, and not the time, you need to create your Date objects in such a way that the times are set the same, typically to midnight when you only care about the date. Using the SimpleDateFormat can accomplish this. Here's a little example program that can get you started in understanding and comparing dates:




Play around with that and let us know if you have any more questions.

And welcome to JavaRanch.
 
Mark Vedder
Ranch Hand
Posts: 624
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
p.s. Working with Dates in Java is a bit confusing at first. That is partly because dealing with Dates and Times is not as simple as it first seems. Throw in Internationalization issues, different calendar systems, different number of days in months, time zones, leap years, leap seconds, some dates not actually existing in history when they made adjustments to the calendar, and it gets complex fast. The Date APIs in Java do a great job at dealing with most of those complexities, but unfortunately add some complexities of their own in doing such. So hang in there. Learning to correctly work with Dates in Java will have huge rewards. Note that there are some external libraries out there that do make working with dates easier. But I think it is important to learn the core Java classes first.
 
Adam Confino
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks again for the explanation. This forum is a great resource.
 
machines help you to do more, but experience less. Experience this tiny ad:
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic