如何在Java中将时间字符串与当前时间进行比较? [英] How to compare time string with current time in java?

查看:922
本文介绍了如何在Java中将时间字符串与当前时间进行比较?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类似 15:30的时间字符串,我想将该字符串与当前时间进行比较。
请提出一些简单的建议。
以及如何以小时分钟格式获取当前时间( HH:mm)

I have a time string like "15:30" i want to compare that string with the current time. Please suggest something easy. And how to get the current time in hour minute format("HH:mm")

推荐答案

tl; dr



tl;dr

LocalTime.now()
         .isAfter( LocalTime.parse( "15:30" ) )



详细信息



您应该考虑另一种方法:如何将字符串转换为时间值。您不会通过将数字转换为字符串来尝试数学。

Details

You should be thinking the other way around: How to get that string turned into a time value. You would not attempt math by turning your numbers into strings. So too with date-time values.

避免使用旧的捆绑类java.util.Date和.Calendar,因为它们非常麻烦,在设计和实现上都存在缺陷。它们被新的 java.time软件包所取代 Java 8 中。 java.time的灵感来自 Joda-Time

Avoid the old bundled classes, java.util.Date and .Calendar as they are notoriously troublesome, flawed both in design and implementation. They are supplanted by the new java.time package in Java 8. And java.time was inspired by Joda-Time.

两个 java.time Joda-Time 报价捕获不具有任何时区的日期的时间的类: LocalTime

Both java.time and Joda-Time offer a class to capture a time-of-day without any date to time zone: LocalTime.

使用Java内置的java.time类,特别是 LocalTime 。获取您当地时区的当前时间。根据您的输入字符串构造一个时间。与 isBefore isAfter isEqual 方法进行比较。

Using the java.time classes built into Java, specifically LocalTime. Get the current time-of-day in your local time zone. Construct a time-of-day per your input string. Compare with the isBefore, isAfter, or isEqual methods.

LocalTime now = LocalTime.now();
LocalTime limit = LocalTime.parse( "15:30" );
Boolean isLate = now.isAfter( limit );

最好指定所需/期望的时区,而不是隐式依赖JVM的当前默认时区。

Better to specify your desired/expected time zone rather than rely implicitly on the JVM’s current default time zone.

ZoneId z = ZoneId.of( "Pacific/Auckland" ) ;
LocalTime now = LocalTime.now( z );  // Explicitly specify the desired/expected time zone.
LocalTime limit = LocalTime.parse( "15:30" );
Boolean isLate = now.isAfter( limit );



Joda-Time



这种使用Joda-Time库的情况恰好与上面针对java.time看到的代码几乎相同。

Joda-Time

The code in this case using the Joda-Time library happens to be nearly the same as the code seen above for java.time.

请注意 Joda-Time 项目现在处于维护模式,团队建议迁移到 java.time 类。

Beware that the Joda-Time project is now in maintenance mode, with the team advising migration to the java.time classes.

java.time 框架内置于Java 8及更高版本中。这些类取代了麻烦的旧旧版日期时间类,例如 java.util.Date Calendar ,& SimpleDateFormat

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

现在处于维护模式的Joda-Time 项目建议迁移到 java.time 类。

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

要了解更多信息,请参见 Oracle教程。并在Stack Overflow中搜索许多示例和说明。规范为 JSR 310

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

从哪里获取java.time类?

Where to obtain the java.time classes?


  • Java SE 8 Java SE 9 ,然后是


    • 内置。

    • 具有捆绑实现的标准Java API的一部分。

    • Java 9添加了一些次要功能和修复。

    • Java SE 8, Java SE 9, and later
      • Built-in.
      • Part of the standard Java API with a bundled implementation.
      • Java 9 adds some minor features and fixes.
      • Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
      • The ThreeTenABP project adapts ThreeTen-Backport (mentioned above) for Android specifically.
      • See How to use ThreeTenABP….

      ThreeTen-Extra 项目使用其他类扩展了java.time。该项目是将来可能向java.time添加内容的试验场。您可能会在这里找到一些有用的类,例如 时间间隔 YearWeek YearQuarter 更多

      这篇关于如何在Java中将时间字符串与当前时间进行比较?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆