简体   繁体   中英

Calendar comparison

I am storing current date in SQLite DB as

created_at DATETIME DEFAULT CURRENT_TIMESTAMP

When I try to compare Calendar objects, its always shows objects as not equal.

Here is my code.

Creating Calendar from Database string

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar calendarDB = Calendar.getInstance();
String date = "extracted only date (2014-03-03 for ex) from DB value ignoring time";
calendarDB.setTime(sdf.parse(date));

Current Calendar instance

Calendar calendarCurrent = Calendar.getInstance();

Comparison... I see not equal for all the instances.

if(calendarDB.equals(calendarCurrent))
    Log.i(TAG, "equal!!!");
else
    Log.i(TAG, "Not equal!!!");

I can see both Calendar instance values like day, month, year equal in Log.

What's wrong with it?

Calendar.getInstance() is not singleton. It every time creates a new instance of different implementations of java.util.Calendar based on Locale

Secondly, if you check equals method of Calendar , it checks lot more, than just the date

public boolean equals(Object obj) {
    if (this == obj)
        return true;
try {
    Calendar that = (Calendar)obj;
    return compareTo(getMillisOf(that)) == 0 &&
    lenient == that.lenient &&
    firstDayOfWeek == that.firstDayOfWeek &&
    minimalDaysInFirstWeek == that.minimalDaysInFirstWeek &&
    zone.equals(that.zone);
} catch (Exception e) {
    // Note: GregorianCalendar.computeTime throws
    // IllegalArgumentException if the ERA value is invalid
    // even it's in lenient mode.
}
return false;
}

If you want to compare 2 dates then, you can do

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date one = new Date(); // one = calOne.getTime();
Date two = new Date(); //two = calTwo.getTime();
sdf.format(one).equals(sdf.format(two));

That's happens because a Calendar equals method compare to next fields :

@Override
public boolean equals(Object object) {
    if (this == object) {
        return true;
    }
    if (!(object instanceof Calendar)) {
        return false;
    }
    Calendar cal = (Calendar) object;
    return getTimeInMillis() == cal.getTimeInMillis()
            && isLenient() == cal.isLenient()
            && getFirstDayOfWeek() == cal.getFirstDayOfWeek()
            && getMinimalDaysInFirstWeek() == cal.getMinimalDaysInFirstWeek()
            && getTimeZone().equals(cal.getTimeZone());
}

So you need to check :

  • millis
  • isLenient
  • first day of week
  • minimal days in first week
  • and time zone

As you can see equals method need a same TimeZone in comaprable object.

tl;dr

LocalDate.now( ZoneId( "Pacific/Auckland" ) )        // Get today’s date in a particular time zone.
    .isEqual(                                        // Test for equality against a `LocalDate` object to be retrieved from database.
        myResultSet.getObject( … , Instant.class )   // Retrieve a moment in UTC from the database, an `Instant` object.
            .atZone( ZoneId( "Pacific/Auckland" ) )  // Produce a `ZonedDateTime` to represent the same moment in time but with the wall-clock time of a particular region’s time zone.
            .toLocalDate()                           // Extract a date-only value, without time-of-day and without time zone.
    )

java.time

The modern approach uses the java.time classes.

With JDBC 4.2 and later, you may directly exchange java.time objects with your database. No need for the troublesome Calendar class, no need for mere strings.

The Instant class represents a moment on the timeline in UTC with a resolution of nanoseconds (up to nine (9) digits of a decimal fraction).

Instant instant = myResultSet.getObject( … , Instant.class ) ;

A time zone is crucial in determining a date. For any given moment, the date varies around the globe by zone. For example, a few minutes after midnight in Paris France is a new day while still “yesterday” in Montréal Québec .

Specify a proper time zone name in the format of continent/region , such as America/Montreal , Africa/Casablanca , or Pacific/Auckland . Never use the 3-4 letter abbreviation such as EST or IST as they are not true time zones, not standardized, and not even unique(!).

ZoneId z = ZoneId.of( "America/Montreal" ) ;

Apply the ZoneId to your Instant to get a ZonedDateTime .

ZonedDateTime zdt = instant.atZone( z ) ;

If you care about only the date portion, and not the time-of-day, extract a LocalDate .

LocalDate ld = zdt.toLocalDate() ;

Compare to the current date.

LocalDate today = LocalDate.now( z ) ;
Boolean isSameDateAsToday = ld.isEqual( today ) ;

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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM