tl;dr
( ( GregorianCalendar ) c1 )
.toZonedDateTime()
.toLocalDate()
.isEqual(
( ( GregorianCalendar ) c2 )
.toZonedDateTime()
.toLocalDate()
)
java.time
The Answer by Jon Skeet is correct. But its suggestion to use Joda-Time is outmoded.
The date-time classes bundled with the earliest versions of Java were simply terrible. For example, Date
& Calendar
and its usual concrete class GregorianCalendar
. The adoption of JSR 310 years supplanted those classes with the modern java.time classes. JSR 310 was led by the man who invented Joda-Time, Stephen Colebourne.
Calendar
➙ GregorianCalendar
➙ ZonedDateTime
➙ LocalDate
If given a Calendar
convert to ZonedDateTime
via GregorianCalendar
.
if( myCal instanceOf GregorianCalendar ) {
GregorianCalendar gc = ( GregorianCalendar ) myCal ;
}
Then convert to its replacement, ZonedDateTime
.
ZonedDateTime zdt = gc.toZonedDateTime() ;
Both GregorianCalendar
& ZonedDateTime
represent a moment, a specific point on the timeline. That moment viewed through two different time zones such as Tokyo Japan versus Toledo Ohio US could land on two different dates.
To represent a date-only, without time-of-day, and without time zone or offset-from-UTC, use LocalDate
class.
LocalDate ld = zdt.toLocalDate() ;
You can compare a pair of LocalDate
objects.
boolean isBefore = localDateX.isEqual( localDateY ) ;
You may want to adjust both ZonedDateTime
objects to the same time zone before considering their date.
ZoneId zNewYork = ZoneId.of( "America/New_York" ) ;
ZonedDateTime zdtXNewYork = zdtX.withZoneSameInstant( zNewYork ) ;
LocalDate localDateX = zdtXNewYork.toLocalDate() ;
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
.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.
You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.*
classes.
Where to obtain the java.time classes?
The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval
, YearWeek
, YearQuarter
, and more.
java.util.Date
,java.util.Calendar
, andjava.text.SimpleDateFormat
are now legacy, supplanted by the java.time classes built into Java 8 and later. – Chivers