[英]How can i know if current time is 5 am in specific time zone
我的系统时区是UTC。 如果在给定时区的凌晨 5 点,我如何知道使用 java 日期时间 api?
如何为此使用区域日期时间?
新加坡现在是早上五点吗? 印度现在是凌晨 5 点吗? 奥地利现在是凌晨五点吗?
谢谢
这是我最后的逻辑。 在解决以下解决方案之后。
public class MorningReminderScheduler {
@Scheduled( cron="0 0/5 * * * * ")
public static void main(String args[]){
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm:ss");
for (String zoneId : ZoneId.getAvailableZoneIds()) {
ZoneId z = ZoneId.of(zoneId);
// LocalDateTime -> ZonedDateTime
LocalTime l= LocalTime.now(z);
if(l.getHour()==5 && l.getMinute()<5){
System.out.println("its between 5 hour to 5 hour 5 min in = " + z.getId());
}
}
}
}
您可以使用 LocalTime class 来查找任何支持时区的当地时间。 例如:
String zoneId = "Asia/Singapore";
LocalTime timeInSingapore = LocalTime.now(ZoneId.of(zoneId));
要确定新加坡是否是凌晨 5 点,您可能需要应用一些阈值,因为在很短的时间内时间正好是凌晨 5 点。 您可以做的是计算早上 5 点与给定时刻之间的差异,如果差异“足够小”,您可以声称它是早上 5 点。 例如,这将声明时间是凌晨 5 点,在 4:50 到 5:10 之间的任何时间:
LocalTime fiveAm = LocalTime.of(5, 0);
long minutesBetween = Math.abs(ChronoUnit.MINUTES.between(fiveAm, timeInSingapore));
if (minutesBetween <= 10) {
// close enough
System.out.println("It's 5am in " + zoneId);
}
您可以使用您想知道时间的时区的 zoneId。 使用 DateFormatter 格式化日期,然后验证它是否为 5:00 AM。 示例代码如下所示:
public static void getCurrentTimeWithTimeZone(){
System.out.println("-----Current time of a different time zone using LocalTime-----");
ZoneId zoneId = ZoneId.of("America/Los_Angeles");
LocalTime localTime=LocalTime.now(zoneId);
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm:ss");
String formattedTime=localTime.format(formatter);
System.out.println("Current time of the day in Los Angeles: " + formattedTime);
if(formattedTime.equals("05:00:00")
System.out.println("It is 5 AM in Los Angeles");
}
假设您说5 am
时的意思是5:00
即您不考虑秒和纳秒,您可以将新加坡的小时和分钟分别与5
和0
进行比较,以评估它是否是5 am
。
import java.time.LocalTime;
import java.time.OffsetTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
public class Main {
public static void main(String[] args) {
// Singapore has the Zone Offset of UTC +8
LocalTime nowAtSingapore = OffsetTime.now().withOffsetSameInstant(ZoneOffset.ofHours(8)).toLocalTime();
System.out.println("Now at Singapore: " + nowAtSingapore);
System.out.println("Now (HH:mm) at Singapore: " + DateTimeFormatter.ofPattern("HH:mm").format(nowAtSingapore));
// Check if it is 5:00 at Singapore now
if (nowAtSingapore.getHour() == 5 && nowAtSingapore.getMinute() == 0) {
System.out.println("It's 5:00 at Singapore");
}
}
}
Output:
Now at Singapore: 05:24:43.217733
Now (HH:mm) at Singapore: 05:24
注意: LocalTime.toString LocalTime.toString()
的默认格式是HH:mm:ss.SSSSSS
,其中SSSSSS
表示纳秒。 为了获取自定义模式中的字符串,我们使用DateTimeFormatter#ofPattern
。
编辑:对于您的 cron 工作,我建议您跟踪您为该时区发送文本(SMS)消息的最后日期。 因此,如果现在已经过了第二天早上5 点,请发送下一条消息(并更新存储的日期)。 这将确保即使作业没有恰好在您的 5 分钟容差间隔内运行,也会发送消息。 如果作业恰好运行两次,也不会发送两次。 时间很棘手,因为我们永远不会让事情在指定的时间准确地发生,所以我们必须考虑到这些可能性。
原始答案
不,它不是印度的凌晨 5 点,也不是您提到的其他时区之一。 某个时区凌晨 5 点的时间点持续为 0,因此该时间点的概率为 0。
在许多平台上 Java 可以自 Java 9 以微秒精度读取时间。 因此,即使不完全是凌晨 5 点,我们也有 0.000000001 % 的概率认为读取的时间是凌晨 5 点。 可以说它几乎永远不会发生。
所以我建议你保持一定的宽容度:如果时间足够接近凌晨 5 点,你就认为它是凌晨 5 点。 例如:
final LocalTime time = LocalTime.of(5, 0); // 5 AM
final Duration tolerance = Duration.ofMillis(600); // 0.6 seconds as an example
ZoneId zone = ZoneId.of("Asia/Kolkata"); // India
LocalTime nowInZone = LocalTime.now(zone);
if (nowInZone.isBefore(time.minus(tolerance)) || nowInZone.isAfter(time.plus(tolerance))) {
System.out.println("No, it is not " + time + " in " + zone);
} else {
System.out.println("Yes, it is " + nowInZone + " in " + zone);
}
刚才运行代码时,output 很可能是:
不,在亚洲/加尔各答不是 05:00
请注意,只要容差不超过午夜(上午 12 点),代码才能正常工作。 例如,时间为 0:01 AM,容差为 2 分钟,我们将检查时间是否在 23:59 之后和0:03 之前。 没有时间可以两者兼而有之,所以即使时间在 4 分钟的间隔内,我们也总是会得到“否”。 但是对于凌晨 5 点和小于 5 小时的容差,它确实有效。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.