Win a copy of Microservices Testing (Live Project) this week in the Spring 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
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

Compare Date.

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


I wish to know how we can perform Date comparison in Java.

Thanks In Advance.
 
Marshal
Posts: 27286
87
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you have two Date objects then you can use their before() and after() methods to see if one is before... or after... the other one.

If you actually have a strings that represent dates, then use a SimpleDateFormat object and call its parse() method to produce Date objects. Then compare the Date objects.
 
MInu
Ranch Hand
Posts: 517
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Paul.

For that i have to seperate year ,month and day .right?

Then compare with two,right?


Any other suggestion.
 
Paul Clapham
Marshal
Posts: 27286
87
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by minu su:
For that i have to seperate year ,month and day. right?

No. The SimpleDateFormat object takes care of parsing a string into a Date. Here is a link to its documentation. It has reasonably good examples.

Then compare with two,right?

I don't understand what that means.

Any other suggestion.

You could write your own string-hacking code that tries to split year, month, and day and so on but when you could just writeI don't know why you would want other suggestions.
 
author
Posts: 4323
39
jQuery Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Depends what you want. If you just want to know which is greater or less, don't use date compare, just convert the date to milliseconds using getTime() then just compare the long values.

If you want more complex comparisons, load the date into a Calendar class and refer to the Calendar API for more information and to learn its features.
 
Did you miss me? Did you miss this tiny ad?
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic