Skip to content

How to compare a ZonedDateTime’s time with another ZonedDateTime but with only time and not date?

I have two dates in my Local Time time.

"2021-09-01 07:00:00 AM" and "2021-09-01 08:00:00 AM" (PST)

I’m trying to check if these two dates are between two times, 8 am and 10 pm of another time zone. (EST)

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss a");
ZonedDateTime startLocal = LocalDateTime.parse("2021-09-01 07:00:00 AM", formatter).atZone(ZoneId.systemDefault());
ZonedDateTime endLocal = LocalDateTime.parse("2021-09-01 08:00:00 AM", formatter).atZone(ZoneId.systemDefault());

ZonedDateTime startEst = startLocal.withZoneSameInstant(ZoneId.of("America/New_York"));
ZonedDateTime endEst = endLocal.withZoneSameInstant(ZoneId.of("America/New_York"));

System.out.println("Start Est: " + startEst.format(formatter)); //Start Est: 2021-09-01 10:00:00 AM
System.out.println("End Est: " + endEst.format(formatter)); //End Est: 2021-09-01 11:00:00 AM

I’m stuck on how I can compare the starting time zones, startEst and endEst with the two times 8 am and 10 pm without the use of a date and only using the time.

I’ve added two more ZonedDateTimes to compare

ZonedDateTime buisnessStartHoursInEst8am = //Unsure of what to use here
ZonedDateTime buisnessEndHoursInEst10pm = //Unsure of what to use here
if(startEst.isBefore(buisnessStartHoursInEst8am)){
     System.out.println("Business has not opened yet");
}

if(endEst.isAfter(buisnessEndHoursInEst10pm)){
     System.out.println("Business has already closed");
}

Answer

There are many problems with your code.

  1. You should parse the strings to LocalDateTime as they do not have a timezone.
  2. Convert them to ZonedDateTime, which you have not done correctly. You should use LocalDateTime#atZone to do so.

Finally, convert the obtained ZonedDateTimes to the ZonedDateTimes in the EST timezone and then do the comparison. For getting the buisnessStartHoursInEst8am and buisnessEndHoursInEst10pm, use ZonedDateTime#withHours.

Do it as follows:

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;

public class Main {
    public static void main(String[] args) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss a", Locale.ENGLISH);
        LocalDateTime startLocal = LocalDateTime.parse("2021-09-01 07:00:00 AM", formatter);
        LocalDateTime endLocal = LocalDateTime.parse("2021-09-01 08:00:00 AM", formatter);

        ZoneId zoneLA = ZoneId.of("America/Los_Angeles");
        ZoneId zoneNY = ZoneId.of("America/New_York");

        ZonedDateTime startZdtPst = startLocal.atZone(zoneLA);
        ZonedDateTime endZdtPst = endLocal.atZone(zoneLA);

        ZonedDateTime startZdtEst = startZdtPst.withZoneSameInstant(zoneNY);
        ZonedDateTime endZdtEst = endZdtPst.withZoneSameInstant(zoneNY);

        ZonedDateTime buisnessStartHoursInEst8am = ZonedDateTime.now(zoneNY).withHour(8);
        ZonedDateTime buisnessEndHoursInEst10pm = ZonedDateTime.now(zoneNY).withHour(22);

        // Compare
        if (startZdtEst.isBefore(buisnessStartHoursInEst8am)) {
            System.out.println("Business has not opened yet");
        }

        if (endZdtEst.isAfter(buisnessEndHoursInEst10pm)) {
            System.out.println("Business has already closed");
        }
    }
}

Learn more about the modern Date-Time API* from Trail: Date Time.