0

I am trying to compare 2 calendar objects with their dates.

My code:

Calendar c1 = Calendar.getInstance(); 
Calendar c2 = Calendar.getInstance(); 

c1.set(2017, 1,2); 
c2.set(2017, 1,1); 

int compared = c1.compareTo(c2);

textView.setText("" + compared);

The result should be an int of either -1 or 1, in which my case should be: -1.

However, this is not the result I want. I want to be able to compare dates directly say:

01/20/2010 compared to 02/30/2014 returns true or false. 

Is there some ways to achieve this based on the Calendar lib?

I am aware of built in methods such as getDay(), getMonth(), equals() and so on. The problem is that in using getDay(), getMonth(), getYear(), it is very difficult to make a comparison as I'm comparing between 3 ints.

Also, I got a feeling that to compare between 2 dates, I will have to set the timezone and the timeinMillis to be the same. Meaning to compare the date direct, for cal1 and cal2, its time zone and timeinmillis has to be the same.

Can someone clarify this to me?

GhostCat
  • 137,827
  • 25
  • 176
  • 248
Positive-One
  • 89
  • 1
  • 12
  • From [the docs: compareTo()](http://docs.oracle.com/javase/8/docs/api/java/util/Calendar.html#compareTo-java.util.Calendar-) returns “the value 0 if the time represented by the argument is equal to the time represented by this Calendar; a value less than 0 if the time of this Calendar is before the time represented by the argument; and a value greater than 0 if the time of this Calendar is after the time represented by the argument.” *Please don’t* count on it being either -1 or 1. – Ole V.V. Apr 21 '17 at 10:03
  • What is the result you want? Should that be true for equal or true for the first being earlier than the second? Or…? – Ole V.V. Apr 21 '17 at 10:07

4 Answers4

2

You should be careful there from a conceptual point of view: compareTo() has that very specific meaning of returning an int, with that - 1, 0,1 results telling you about how to order the compared objects.

That boolean result you are looking for indicates that you should think/speak using verbs like equals, before, or after!

It is a bad idea to take a well defined concept, keeping the name but changing the meaning under the covers!

GhostCat
  • 137,827
  • 25
  • 176
  • 248
1

you could use either Calendar#before or Calendar#after to make a comparison which returns a boolean.

SimpleDateFormat formatC1 = new SimpleDateFormat("yyyy-MM-dd");
String formattedC1 = formatC1.format(c1.getTime());
SimpleDateFormat formatC2 = new SimpleDateFormat("yyyy-MM-dd");
String formattedC2 = formatC2.format(c2.getTime());
textView.setText(formattedC1 + " compared to " + formattedC2 + " returns " + c1.before(c2));
Ousmane D.
  • 54,915
  • 8
  • 91
  • 126
  • I did used the before() and after() built in functions! It did not however, returned my desired answers. I however did not realize that the getTime() then format it using SimpleDateTimeFormatter does the trick! thanks for all your answers. – Positive-One Apr 21 '17 at 03:28
1

tl;dr

LocalDate.of( 2010 , 1 , 20 )
         .isBefore( 
             LocalDate.of( 2014 , 2 , 30 ) 
         )

true

Details

You are using the wrong method on the wrong class.

The Calendar class represents a date and time-of-day. But you want a date-only value without a time-of-day.

You are calling the compareTo method which is defined by a specific interface Comparable with a specific purpose. This method is designed to return integers whereas you want a boolean result of "isBefore" or "isAfter".

Using java.time

Also, the troublesome Calendar class is one of the old date-time classes that are now legacy, supplanted by the java.time classes.

The LocalDate class represents a date-only value without time-of-day and without time zone.

LocalDate ld1 = LocalDate.of( 2010 , 1 , 20 ) ;
LocalDate ld2 = LocalDate.of( 2014 , 2 , 30 ) ;

Compare with boolean methods.

boolean ld1IsBefore = ld1.isBefore( ld2 );
boolean ld1IsAfter = ld1.isAfter( ld2 );
boolean ld1IsEqual = ld1.isEqual( ld2 );

About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

Where to obtain the java.time classes?

Community
  • 1
  • 1
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
0

you can do it this way

textView.setText("c1 compared to c2 returns " + (c1.compareTo(c2) == 0));

As per the javadocs

Compares the time values (millisecond offsets from the Epoch) represented by two Calendar objects.

Test

        Calendar c1 = Calendar.getInstance();
        Calendar c2 = Calendar.getInstance();
        c2.add(Calendar.MINUTE, 1);

        System.out.println ("c1 compared to c2 returns " + (c1.compareTo(c2) == 0));
Scary Wombat
  • 44,617
  • 6
  • 35
  • 64