-2

I have a From date and a To date in the application. I have to allow the user to enter the date like below.

example:

From Date :01-01-2014 To Date :01-02-2014
From Date :02-02-2014 To Date :01-03-2014

and I am not supposed to allow the any date between like this

From Date: 05-01-2014 To date :30-01-2014

How do I validate the dates like this in java.

Idistic
  • 6,281
  • 2
  • 28
  • 38
  • Have you tried anything so far? Can you post some code? – Totò Apr 30 '14 at 07:01
  • You could make a simple comparison like this `if(FromDate > ToDate) return false` – Santhosh Apr 30 '14 at 07:03
  • After you add some code or show some previous effort to solve your problem, best answer you can receive it's "Carefully my friend, carefully" – JavierV Apr 30 '14 at 07:03
  • Please explain what the exact rules are for allowable dates. I don't see any issue with what you have shown (5th January is before 30th January). – Jason Apr 30 '14 at 07:06

2 Answers2

0

The Date API has a a compareTo method : http://docs.oracle.com/javase/7/docs/api/java/util/Date.html#compareTo%28java.util.Date%29 returning the usual -1,0,+1 values

rubenafo
  • 509
  • 1
  • 7
  • 23
0

Standard Date class in Java have simple before and after methods, that is what you need.

First of all get input, parse it with SimpleDateFormat, for example, and finally compare dates.

There are compareTo method, more C-like style, with int return.

TSB99X
  • 3,260
  • 2
  • 18
  • 20