user195257
user195257

Reputation: 3316

Compare date Java format problem

Trying to compare some dates in java but can't get the formatting right, where am i going wrong?

DateFormat df = new SimpleDateFormat("dd/mm/yyyy");
    Date date1 = null, date2 = null, today = new Date();
date1 = (Date) df.parse(scan.next());

System.out.println(date1);
            System.out.println(today);
if(date1.compareTo(today) < 0){
        date1 = null;
        System.out.println(start + " is not a valid date.. please try again!");
        }

Please enter a start date:
10/04/2011
Mon Jan 10 00:04:00 GMT 2011
Tue Apr 05 22:27:44 BST 2011

Upvotes: 1

Views: 1141

Answers (2)

Steven Mastandrea
Steven Mastandrea

Reputation: 2772

Change line 1 to be: DateFormat df = new SimpleDateFormat("dd/MM/yyyy");

mm in SimpleDateFormat is the minutes. MM is the month. So your input is actaully January 10 2011 at 00:10:00

Check out http://download.oracle.com/javase/1.4.2/docs/api/java/text/SimpleDateFormat.html for abbreviations and javadoc.

Upvotes: 0

Brian Agnew
Brian Agnew

Reputation: 272267

I think you need MM, not mm

From the doc:

M Month in year
m Minute in hour

Upvotes: 4

Related Questions