?
Solved

compare date strings in java

Posted on 2004-09-08
8
Medium Priority
?
299 Views
Last Modified: 2010-03-31
hai,

i have "startDate" and "endDate" and getting them in request.getParameter(). Now i want to compare the date difference between them.

Just want to verfiy if endDate is greater than startDate.
0
Comment
Question by:ldbkutty
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 37

Accepted Solution

by:
zzynx earned 1600 total points
ID: 12004230
Date startDate = ...
Date endDate = ...

if ( endDate.after(startDate) ) {
   // do your thing
}
0
 
LVL 9

Assisted Solution

by:Nick_72
Nick_72 earned 200 total points
ID: 12004245
with dates you can use the compareTo method, since they are always of the same length:

if (startDate.compareTo(endDate) < 0)
   //startDate is less (or earlier) than endDate

if (startDate.compareTo(endDate) > 0)
   //the opposite

if (startDate.compareTo(endDate) == 0)
  //both dates are equal

/Nick



0
 
LVL 12

Assisted Solution

by:Giant2
Giant2 earned 200 total points
ID: 12004252
If they are String you could change them in the format:
yyy/mm/dd and after this you can use the comparison between String.
0
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 
LVL 12

Expert Comment

by:Giant2
ID: 12004254
:)

yyyy/mm/dd
0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 12004270
String format="MM/dd/yyyy"
DateFormat myDateFormat = new SimpleDateFormat(strFormat);
Date startdate= null;
Date enddate=null;
try {
     startdate= myDateFormat.parse(request.getParameter("startdate"));
     enddate= myDateFormat.parse(request.getParameter("enddate")
} catch (ParseException e) {
     System.out.println("Invalid Date Parser Exception ");
     e.printStackTrace();
}
0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 12004271
oops already accepted
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12004276
>> getting them in request.getParameter()
Guess you get them as String then.

String startDate = ...;
String endDate = ...;

SimpleDateFormat fmt = new SimpleDateFormat("yyyy/mm/dd");   // or whatever the format may be
java.util.Date start = fmt.parse(startDate);
java.util.Date end = fmt.parse(endDate);
if ( end.after(start) ) {
   System.out.println("The end date is greater than the start Date");
}
else if ( end.before(start) ) {
   System.out.println("The end date is smaller than the start Date");
}
else {
   System.out.println("The end date equals the start Date");
}
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12004289
>> oops already accepted
Same feeling ;°)

Thanks for accepting ldbkutty.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Suggested Courses
Course of the Month13 days, 8 hours left to enroll

800 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question