0

How do I compare dates in Java using comparison operators?

Example:

date1 is 30-10-2017
date2 is 31-10-2017
date3 is 30-10-2018

date2 returns false when it should be true that it is less than date3. How can I return true if the date is less than another date and false otherwise?

This is my code:
return (day < theDate.day) || (month < theDate.month) || (year < theDate.year);

Below is my current solution:

{
  boolean check = false;
  if(year < theDate.year)
  {
      return true;
  }
  if(year > theDate.year)
  {
      return false;
  }
  if(month < theDate.month)
  {
      check = true;
  }
  if(day < theDate.day)
  {
      check = true;
  }
  else
  {
      return false;
  }
  return check;
}
2
  • I don't think java accepts comparison operators on dates. but you can use the LocalDate API along with the isBefore and isAfter methods. Commented Oct 7, 2017 at 18:19
  • What is the type of your date-class? (fully qualified) Is it java.util.Date, or is it java.time.LocalDate or something else? Commented Oct 7, 2017 at 18:38

3 Answers 3

1
LocalDate date1 = LocalDate.of(2017, 10, 30); // Year, month, day.
LocalDate date2 = LocalDate.of(2017, 10, 31);
LocalDate date3 = LocalDate.of(2018, 10, 30);

System.out.printf("%s before %s? %s%n", date1, date2, date1.isBefore(date2));
System.out.printf("%s before %s? %s%n", date2, date3, date3.isBefore(date3));
System.out.printf("%s before %s? %s%n", date3, date1, date3.isBefore(date1));

If you want to know how to do a comparison yourself:

  • Compare first year, then month, then day: from most significant to least
  • For this reason the ISO standard of denoting dates (and times) is 2017-09-30T13:05:01,000 where a text comparison with a representation of the same length suffices.

And the pattern goes:

  1. if years not equal then result found
  2. if months not equal then result found
  3. result is comparison of days
Sign up to request clarification or add additional context in comments.

Comments

0

You can't use relational operators directly, but date/time types typically implement Comparable.

So you should be able to do date1.compareTo(date2) < 0 for logical date1 < date2.

3 Comments

I am comparing numeric values I set for the date objects. The fields I am using are integers for day, month, and year. So I am trying to check those values.
@julianrios I don't understand what you're trying to say.
This is an assignment for my intro to java class. So I'm just a bit lost. I am coding in bluej and I don't believe I can access api for this assignment.
0

You should use boolean after(Date date) or boolean before(Date date) for that. For example, date2.before(date3)

4 Comments

Sorry I don't understand.
@julianrios What class do you use for dates?
Is there a way to insert my all of my code? It may be easier to just look at what I have in my fields and methods.
@julianrios You can push "edit" under your question and add something

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.