WHOATEMYNOODLES
WHOATEMYNOODLES

Reputation: 2643

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");
}

Upvotes: 1

Views: 3665

Answers (3)

adam
adam

Reputation: 125

Your code is quite ambiguous but I will try to give you an example of how I can check the difference of the same time in different zone in addition to printing Time only

Create two Zone ids

create a LocalDateTime

create ZoneDateTime which need both the zone and LocalDateTime. after that just invoke withZoneSameInstant and choose other Zone,

here it will print full LocalDateTime once again invoke toLocalTime to get the time only


        ZoneId nYork = ZoneId.of("America/New_York");
        ZoneId tokyo = ZoneId.of("Asia/Tokyo");

        LocalDateTime randomLDT = LocalDateTime.of(2021, 9, 22, 07, 00);

        ZonedDateTime selectFirstZone = ZonedDateTime.of(randomLDT, nYork);

        System.out.println(selectFirstZone.withZoneSameInstant(tokyo).toLocalTime());



Upvotes: 0

Basil Bourque
Basil Bourque

Reputation: 340070

You said:

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)

That first sentence appears to be a misunderstanding. A LocalTime object holds only a time-of-day, without a date. Perhaps you meant LocalDateTime, which holds a date and time-of-day, but lacks the context of a time zone or offset-from-UTC.

While your the body of your Question is unclear, let's address the specifics of your title:

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

To compare to ZonedDateTime objects by their time-of-day only, ignoring the date and ignoring the time zone, just extract LocalTime objects.

ZonedDateTime edmonton = ZonedDateTime.now( ZoneId.of( "America/Edmonton" ) ) ;
ZonedDateTime tunis = ZonedDateTime.now( ZoneId.of( "Africa/Tunis" ) ) ;

LocalTime edmontonTime = edmonton.toLocalTime() ;
LocalTime tunisTime = tunis.toLocalTime() ;

boolean isEdmontonEarlierThanTunis = edmontonTime.isBefore( tunisTime ) ;  

See this code run live at IdeOne.com.

2021-09-01T15:13:55.380552-06:00[America/Edmonton]
2021-09-01T22:13:55.384061+01:00[Africa/Tunis]
isEdmontonEarlierTimeThanTunis: true  edmontonTime: 15:13:55.380552 tunisTime: 22:13:55.384061

Upvotes: 3

Arvind Kumar Avinash
Arvind Kumar Avinash

Reputation: 79560

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.

Upvotes: 3

Related Questions