File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes dates, strings, and confusion Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "dates, strings, and confusion" Watch "dates, strings, and confusion" New topic
Author

dates, strings, and confusion

Adam Confino
Ranch Hand

Joined: Sep 03, 2009
Posts: 48
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!


Just Another Guy Hooked on Java
Nishan Patel
Ranch Hand

Joined: Sep 07, 2008
Posts: 689

Hi Adam,

welcome to java ranch ....

Pleace code tag when your question contains code.

read this Use Code Tags ..


Thanks, Nishan Patel
SCJP 1.5, SCWCD 1.5, OCPJWSD Java Developer,My Blog
D. Ogranos
Ranch Hand

Joined: Feb 02, 2009
Posts: 214
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

Joined: Sep 03, 2009
Posts: 48
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.
Mark Vedder
Ranch Hand

Joined: Dec 17, 2003
Posts: 624

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

Joined: Dec 17, 2003
Posts: 624

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

Joined: Sep 03, 2009
Posts: 48
Thanks again for the explanation. This forum is a great resource.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: dates, strings, and confusion