Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

compare date strings in java

Posted on 2004-09-08
8
Medium Priority
?
308 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
Independent Software Vendors: 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!

 
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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

604 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