1

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
2
  • Just a suggestion, but take a look at JodaTime. I know this doesn't answer your question, but JodaTime provides a lot of nifty features to handle dates. Commented Apr 5, 2011 at 21:39
  • While JodaTime is nice, isn't is a bit of overkill for a compareTo(...)? Commented Apr 5, 2011 at 21:42

2 Answers 2

4

I think you need MM, not mm

From the doc:

M Month in year
m Minute in hour

Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.