| Author |
Date comparision in java
|
Tusshar Fasate
Ranch Hand
Joined: May 21, 2008
Posts: 81
|
|
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
Bartender
Joined: Aug 16, 2005
Posts: 12953
|
|
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.
|
Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
|
 |
Tusshar Fasate
Ranch Hand
Joined: May 21, 2008
Posts: 81
|
|
Thanks for reply.
But this is the case i want.
Is there any other suggestions...?
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19232
|
|
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:
|
SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
|
 |
Jesper de Jong
Java Cowboy
Bartender
Joined: Aug 16, 2005
Posts: 12953
|
|
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
Joined: Apr 08, 2010
Posts: 8
|
|
cheers
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16815
|
|
This is a past similar topic, if you are interested...
http://www.coderanch.com/t/482942/java/java/parse-user-entered-Date-Date
Henry
|
Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
|
 |
 |
|
|
subject: Date comparision in java
|
|
|