0

So, I want to compare two dates and I have an if loop to function if one date is after the other. My method:

Start and End Dates are user inputted variables and are in the form Month/Day/Year.

String startDate = "02/20/2012";
String endDate = "03/20/2012";

Date startDate1 = new SimpleDateFormat("MM/dd/yyyy").parse(startDate);
Date endDate1 = new SimpleDateFormat("MM/dd/yyyy").parse(endDate);

however, startDate1 is showing this: Mon Feb 20 00:00:00 EST 2012 and, endDate1 is showing this: Tue Mar 20 00:00:00 EDT 2012

I don't understand what i'm doing wrong here.

The loop: if (endDate1.after(startDate1)){ blah blah blah }

5
  • 5
    i don't understand. these result seem correct to me. What would you expect? Commented Apr 16, 2014 at 20:54
  • 1
    Can you explain what you think is wrong with those dates? Because it's not obvious. Note one is in EST and the other in EDT because of daylight savings. Commented Apr 16, 2014 at 20:55
  • I was under the assumption that it would change it into a Date format so startDate1 would still be 02/20/2012 but would be in Date Format....I didn't think that it would write it out... Commented Apr 16, 2014 at 20:57
  • @user2122268 Instead of assuming, you should have searched StackOverflow for "java date". You would quickly learn that java.util.Date has no time zone yet its toString method confusingly applies the JVM's default time zone when generating the string. Commented Apr 16, 2014 at 21:08
  • A Date represents a moment in time. It's not a String - it doesn't have a format of its own. You are successfully converting your Strings into Dates. Nothing is wrong with your code. Commented Apr 16, 2014 at 21:41

4 Answers 4

2

You're not doing anything wrong, that's the default toString() representation format of the Date object in java

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

Comments

0

Use this to compare two dates.

if(startDate1.compareTo(endDate1)>0){
     System.out.println("Date1 is after Date2");
}else if(startDate1.compareTo(endDate1)<0){
     System.out.println("Date1 is before Date2");
}else if(startDate1.compareTo(endDate1)==0){
     System.out.println("Date1 is equal to Date2");
}else{
     System.out.println("How to get here?");
     }

1 Comment

The OP is using after to compare the two dates, which is far more readable than compareTo. Using this code would be a step backwards.
0

What you specified with MM/dd/yyyy is the input format to correctly convert user input to a date. What is "shown" (by the debugger ?) is the output format as returned by the toString method.

But the date is actually stored as an integer number of milliseconds elapsed since January 1, 1970, 00:00:00 GMT.

Comments

0

You are seeing correct behavior. The java.util.Date object has both a date portion and a time portion. The object has no time zone yet its toString method confusingly applies the JVM's default time zone when generating the string.

The java.util.Date and .Calendar classes bundled with Java are notoriously troublesome. Avoid them. Use a decent date-time library. For Java, that means either Joda-Time or the new java.time package in Java 8 (inspired by Joda-Time, defined by JSR 310).

Joda-Time offers a LocalDate class if you are certain you do not need time or time zone. Caution: Naïve programmers often think they have no need for time or time zone but later regret that decision.

Example code using Joda-Time 2.3.

String startInput = "02/20/2012";
String stopInput = "03/20/2012";
DateTimeFormatter formatter = DateTimeFormat.forPattern( "MM/dd/yyyy" );

LocalDate start = formatter.parseLocalDate( startInput );
LocalDate stop = formatter.parseLocalDate( stopInput );
boolean isStopAfterStart = stop.isAfter( start );

Dump to console…

System.out.println( "start: " + start );
System.out.println( "stop: " + stop );
System.out.println( "isStopAfterStart: " + isStopAfterStart );

When run…

start: 2012-02-20
stop: 2012-03-20
isStopAfterStart: true

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.