Solved

compare date strings in java

Posted on 2004-09-08
8
255 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
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 37

Accepted Solution

by:
zzynx earned 400 total points
Comment Utility
Date startDate = ...
Date endDate = ...

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

Assisted Solution

by:Nick_72
Nick_72 earned 50 total points
Comment Utility
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 50 total points
Comment Utility
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
 
LVL 12

Expert Comment

by:Giant2
Comment Utility
:)

yyyy/mm/dd
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 14

Expert Comment

by:sudhakar_koundinya
Comment Utility
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
Comment Utility
oops already accepted
0
 
LVL 37

Expert Comment

by:zzynx
Comment Utility
>> 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
Comment Utility
>> oops already accepted
Same feeling ;°)

Thanks for accepting ldbkutty.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

743 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now