• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Date comparision in java

 
Tusshar Fasate
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • 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.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15219
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Thanks for reply.
But this is the case i want.
Is there any other suggestions...?
 
Rob Spoor
Sheriff
Pie
Posts: 20514
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • 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
Saloon Keeper
Posts: 15219
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • 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.
 
Jiten Kothari
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


cheers
 
Henry Wong
author
Marshal
Pie
Posts: 21024
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is a past similar topic, if you are interested...

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

Henry
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic