Solved

compare date strings in java

Posted on 2004-09-08
8
293 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 400 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 50 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 50 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
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
tomcat startup error 5 160
by zero exception 10 79
Notify sent to other threads in Java 9 53
Java class and jar 3 58
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…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
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 covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses

739 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