Explain Codes LogoExplain Codes Logo

I want to get Year, Month, Day, etc from Java Date to compare with Gregorian Calendar date in Java. Is this possible?

java
date-comparison
java-time-api
best-practices
Nikita BarsukovbyNikita Barsukov·Jan 9, 2025
TLDR

Say you're in a hurry. Here's the quickest snippet to make that Date -> Calendar transition, extract the year, month and day, and then side-by-side this with another GregorianCalendar.

Calendar cal = new GregorianCalendar(); cal.setTime(new Date()); // Convert Date. A.K.A "Battery included!" int year = cal.get(Calendar.YEAR); // Eureka! Got the year. int month = cal.get(Calendar.MONTH); // The month, Jan = 0 (So December isn't 12! 😉) int day = cal.get(Calendar.DAY_OF_MONTH); // Here comes the sun (Day). // Assuming anotherCal is a GregorianCalendar. // Did we just became best friends? Check if they match. boolean sameYear = year == anotherCal.get(Calendar.YEAR); boolean sameMonth = month == anotherCal.get(Calendar.MONTH); boolean sameDay = day == anotherCal.get(Calendar.DAY_OF_MONTH);

Key Points: The get() of Calendar finds the YEAR, MONTH, DAY_OF_MONTH for you. Remember, months are sure playing a prank starting at 0.

Embrace the java.time.LocalDate for efficient solutions

When it became clear that some Date methods belong with the dinosaurs, java.time.LocalDate became the knight in shining armor from Java 8 onwards.

LocalDate localDate = new Date().toInstant() .atZone(ZoneId.systemDefault()) .toLocalDate(); // "Date, meet LocalDate. You guys will handle comparisons well." LocalDate anotherDate = LocalDate.of(2021, Month.JANUARY, 25); boolean isSameDay = localDate.isEqual(anotherDate); // The big question: "Are we equals?"

Handle time zones with care. They are notorious for messing up with the best-laid plans.

Understand the need of using localized methods when time means everything.

Difference? java.time.Period shows the way

Has the 'How many days apart?' question ever kept you awake? java.time.Period comes to the rescue. Your hands needn't get dirty with manual calculations.

Period period = Period.between(anotherDate, localDate); // The middle-man of date difference. System.out.printf("Difference is %d years, %d months, and %d days", period.getYears(), period.getMonths(), period.getDays());

Just another reason why java.time is way cool. It keeps date operations intuitive and trouble-free more than ever.

A calendar year isn't just a year!

Using SimpleDateFormat, remember to choose your year symbols carefully. yyyy is for calendar year and YYYY is for week year.

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); // No Y2K bug here. String dateString = sdf.format(new Date()); // Viola! Your date string plays the symphony.

Don't let ISO week date system standards surprise you with unexpected results.

Future-proof code, no metal rust here!

Take note fellow time-travelers! When coding for future you want it to last and adapt to changes in the Java platform. Wave your wands and magic java.time.* classes turn to gold, replacing the straw of java.util.Date and java.util.Calendar.

  • Migrate: Replace Date with LocalDate.
  • Avoid: Use Calendar.get() or LocalDate.
  • Precise: Be explicit with ZoneId.
  • Efficient: Period and Duration lessen manual hiccups.

With these, you'll like the magic mirror reflecting maintainable and clean code.