• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

String date and Object date comparisons

 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
I have a string object of the format "mm/dd/yyyy" eg. 01/17/2002
I need to compare it with the current date

When I convert the current date to a string of format mm/dd/yyyy using simple date format i seem to be getting inconsistent results.

like 33/17/2002 and 13/17/2002.

Can somebody help.

Thank You
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
From the API doc:


Symbol Meaning
------ -------
m minute in hour
M month in year


Hope this helps.
 
Ranch Hand
Posts: 233
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
You have bug in the code. mm in the formating string will indicate min part of time hence you are getting 33 or 13 . YOu have to change it to MM to get the month of the date object.
Hence the format string will be MM/dd/yyyy.
Try this you will get the required result for details refer API.
-arun
 
Marilyn Monickam
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
Many thanks to Arun and ilja for pointing out.
Thank You
Would it be better for
1) The string object to be converted to a Date
object while comparing or
2) The Date object to be converted to a String object.
and for what reason?
 
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Marilyn,
here is what i think,
in case-1 would be helpful if we just want to see if the given date is matching with the current date or not.
case-2 would be helpful if we want to know if a given date was before/after the current date.
if we have requirements like case-2 then we should not convert Date to String as in case-1 because we would require to implement a logic to see if the date was before/after the current date, right?
thanks
maulin.
 
Ilja Preuss
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also, if you convert the Date to String and for some reasons the format of the String changes, they simply won't be equals; whereas, if you convert the String to Date, there would be the chance to get a parse error should the format change.
 
reply
    Bookmark Topic Watch Topic
  • New Topic