Solved

compare date strings in java

Posted on 2004-09-08
8
287 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
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
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

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
use lov values 2 72
What's wrong with this code? 4 34
JAVA API design with micro service cloud in mind 1 77
Using jdbcTemplate.batchUpdate to improve INSERT performance 6 27
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…
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…
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:
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

756 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