Reputation: 99
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?
Upvotes: 0
Views: 1243
Reputation: 340118
LocalDate.of( 2010 , 1 , 20 )
.isBefore(
LocalDate.of( 2014 , 2 , 30 )
)
true
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".
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 );
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?
Upvotes: 1
Reputation: 140613
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!
Upvotes: 2
Reputation: 44854
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));
Upvotes: 0
Reputation: 56469
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));
Upvotes: 1