• 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
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Date comparision in java

 
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ,
I want to compare two dates.
Out of two dates ,I know the fomat of one date
but I dont know the format of second date.
So how can I do comparision between these two dates.
Thanks.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So, you have those dates as strings? First, you'll have to convert them to java.util.Date objects. You can do that using java.text.SimpleDateFormat (read the API documentation for that class).

But if you don't know what the format of the second date is, it is going to be very hard. The computer can't magically figure that out for you... Note that dates can be written in ambiguous ways: for example, does "01/03/10" mean 1 March 2010, 3 January 2010 or maybe even 10 March 2001?

Once you have the dates as Date objects, you can use methods such as after() and before() on the Date objects (again, lookup the API documentation) to compare them.
 
Tusshar Fasate
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for reply.
But this is the case i want.
Is there any other suggestions...?
 
Sheriff
Posts: 22853
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The main question is, why don't you know the second format? Where is this date coming from? If it is from user input, simply limit the user to a format you know. You can even allow a select group of formats. For instance:
 
Jesper de Jong
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

tushartrue fasate wrote:Thanks for reply.
But this is the case i want.
Is there any other suggestions...?


First you have to answer the following question for yourself: What are you going to do if you get for example "01/03/10" for the second date? You'll have to decide for yourself how that should be handled; we can't tell you, because we don't know the requirements for the program you're writing. Without an answer to that question, it makes no sense to ask for Java code.
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


cheers
 
author
Posts: 23959
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is a past similar topic, if you are interested...

https://coderanch.com/t/482942/java/java/parse-user-entered-Date-Date

Henry
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic