Monday 20 April 2020

Compare two dates without time portion

'java.util.Date' represent date + time + timezone whereas 'java.time.LocalDate' represent only date portion.

Approach 1: Compare year, month and day
public static boolean isDatesEqual_1(Date date1, Date date2) {
         return date1.getYear() == date2.getYear() && date1.getMonth() == date2.getMonth() && date1.getDate() == date2.getDate();
}

Approach 2: Using SimpleDateFormat
public static boolean isDatesEqual_2(Date date1, Date date2) {
         SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
         return simpleDateFormat.format(date1).equals(simpleDateFormat.format(date2));
}

Approach 3: Using LocalDate
public static boolean isDatesEqual_3(Date date1, Date date2) {
         Instant instant1 = date1.toInstant();
         Instant instant2 = date2.toInstant();

         ZoneId zoneId = ZoneId.systemDefault();

         ZonedDateTime zonedDateTime1 = instant1.atZone(zoneId);
         ZonedDateTime zonedDateTime2 = instant2.atZone(zoneId);

         LocalDate localDate1 = zonedDateTime1.toLocalDate();
         LocalDate localDate2 = zonedDateTime2.toLocalDate();

         return localDate1.isEqual(localDate2);
}

Find the below working application.

App.java
package com.sample.app;

import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Date;
import java.util.concurrent.TimeUnit;

public class App {

	public static boolean isDatesEqual_1(Date date1, Date date2) {
		return date1.getYear() == date2.getYear() && date1.getMonth() == date2.getMonth()
				&& date1.getDate() == date2.getDate();
	}

	public static boolean isDatesEqual_2(Date date1, Date date2) {
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
		return simpleDateFormat.format(date1).equals(simpleDateFormat.format(date2));
	}

	public static boolean isDatesEqual_3(Date date1, Date date2) {
		Instant instant1 = date1.toInstant();
		Instant instant2 = date2.toInstant();

		ZoneId zoneId = ZoneId.systemDefault();

		ZonedDateTime zonedDateTime1 = instant1.atZone(zoneId);
		ZonedDateTime zonedDateTime2 = instant2.atZone(zoneId);

		LocalDate localDate1 = zonedDateTime1.toLocalDate();
		LocalDate localDate2 = zonedDateTime2.toLocalDate();

		return localDate1.isEqual(localDate2);
	}

	public static void main(String args[]) throws InterruptedException {

		Date date1 = new Date();
		TimeUnit.SECONDS.sleep(2);
		Date date2 = new Date();
		TimeUnit.SECONDS.sleep(2);

		LocalDate localDate = LocalDate.of(2017, 10, 19);
		Date date3 = Date.from(localDate.atStartOfDay(ZoneId.systemDefault()).toInstant());

		System.out.println("date1 : " + date1);
		System.out.println("date2 : " + date2);
		System.out.println("date3 : " + date3);

		System.out.println("date1 == date2 : " + isDatesEqual_1(date1, date2));
		System.out.println("date1 == date3 : " + isDatesEqual_1(date1, date3));
	}
}

Output
date1 : Sat Mar 07 10:44:52 IST 2020
date2 : Sat Mar 07 10:44:54 IST 2020
date3 : Thu Oct 19 00:00:00 IST 2017
date1 == date2 : true
date1 == date3 : false



You may like
Previous                                                    Next                                                    Home

No comments:

Post a Comment