【问题标题】:How to compare a ZonedDateTime's time with another ZonedDateTime but with only time and not date?如何将 ZonedDateTime 的时间与另一个 ZonedDateTime 但只有时间而不是日期进行比较?
【发布时间】:2021-10-31 08:19:42
【问题描述】:

我的当地时间有两个日期。

"2021-09-01 07:00:00 AM""2021-09-01 08:00:00 AM"(太平洋标准时间)

我正在尝试检查这两个日期是否介于另一个时区的 8 am10 pm 之间。 (美国东部标准时间)

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

我不知道如何比较起始时区startEstendEst 与两次8 am10 pm,而不使用date,而只使用时间。

我又添加了两个ZonedDateTimes 进行比较

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

【问题讨论】:

  • ...在某种程度上,您只能可以比较时间和附加的日期 - 请记住在美国 DST 是一个事物,因此无论给定时间是否介于两者之间另一个时区的另外两个将取决于给定的日期。如果您尝试执行诸如显示本周的打开/关闭时间表之类的操作,您将创建给定日期的开始/结束时间,并使用列出的方法进行检查 - 这就是您想要的做什么?

标签: java zoneddatetime


【解决方案1】:

你说:

我的当地时间有两个日期。

“2021-09-01 07:00:00 AM”和“2021-09-01 08:00:00 AM”(太平洋标准时间)

第一句话似乎是一个误解。 LocalTime 对象仅包含时间,没有日期。也许您的意思是 LocalDateTime,它包含日期和时间,但缺少时区或与 UTC 的偏移量的上下文。

虽然你的问题的主体不清楚,但让我们来谈谈你的标题的细节:

如何将 ZonedDateTime 的时间与另一个 ZonedDateTime 的时间进行比较,但只有时间而不是日期?

要仅按时间与ZonedDateTime 对象进行比较,忽略日期和时区,只需提取LocalTime 对象。

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

看到这个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

【讨论】:

  • 我不相信你已经回答了这个问题。 OP 想要将他的 ZonedDateTime 从一个时区转换为不同区域的 LocalTime,以便进行比较。
  • 我认为他想比较瞬间,即将当地日期调整为 EST。
  • @DawoodibnKareem 标题很清楚,而正文不是:如何将 ZonedDateTime 的时间与另一个 ZonedDateTime 的时间进行比较,但只有时间而不是日期? 我回答了这个问题。也许我应该简单地投票关闭,而不是发布答案。
【解决方案2】:

你的代码有很多问题。

  1. 您应该将字符串解析为LocalDateTime,因为它们没有时区。
  2. 将它们转换为ZonedDateTime,您没有正确完成。您应该使用 LocalDateTime#atZone 来执行此操作。

最后将得到的ZonedDateTimes转换为EST时区的ZonedDateTimes,然后进行比较。要获取buisnessStartHoursInEst8ambuisnessEndHoursInEst10pm,请使用ZonedDateTime#withHours

按如下方式进行:

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

通过 Trail: Date Time 了解有关 modern Date-Time API* 的更多信息。

【讨论】:

    【解决方案3】:

    你的代码很模糊,但我会试着给你一个例子,除了打印时间之外,我如何检查不同区域的同一时间的差异

    创建两个区域 ID

    创建一个本地日期时间

    创建需要区域和 LocalDateTime 的 ZoneDateTime。 之后只需调用 withZoneSameInstant 并选择其他区域,

    这里它将打印完整的 LocalDateTime 再次调用 toLocalTime 以仅获取时间

    
            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());
    
    
    
    

    【讨论】:

      猜你喜欢
      • 2019-01-13
      • 2011-07-29
      • 1970-01-01
      • 2013-01-16
      • 1970-01-01
      • 1970-01-01
      • 2018-09-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多