如何检查当前时间实例之前或之后的截止时间?
How to check cutoff time before or after the current time instance?
目前我正在处理运输条件。在此我将 cut off time
反对公司,如 (05.00 PM)。
现在我想比较上面的时间和当前时间是在 cut off time
之前还是在 cut off time
之后?
我已经浏览了所有 link 我只能看到 date
的示例。随着时间的推移我找不到任何东西。
请让我知道或提供一些线索,以便我进行整理。
这是我目前尝试过的方法
String todayDate=LocalDate.now().toString("dd.MM.yyyy");
String s=todayDate+cutOffTime;//cutOffTime will get from DB
SimpleDateFormat simpleDateFormat=new SimpleDateFormat("dd.MM.yyyy HH:mm a");
LocalDate despatchDate=LocalDate.now();
try {
Date cutoffDate=simpleDateFormat.parse(s);
if (cutoffDate.after(Calendar.getInstance().getTime())){
despatchDate.plusDays(1);
}
} catch (ParseException e) {
e.printStackTrace();
}
Java 8 date/time api
LocalDateTime currentDateTime = LocalDateTime.now();
LocalDate currentDate = LocalDate.now();
String cutOff = "05:00 AM";
DateTimeFormatter timeParser = DateTimeFormatter.ofPattern("hh:mm a");
LocalTime cutOffTime = timeParser.parse(cutOff, LocalTime::from);
LocalDateTime cutOffDateTime = LocalDateTime.of(currentDate, cutOffTime);
//After
cutOffDateTime.isAfter(currentDateTime);
//Before
cutOffDateTime.isBefore(currentDateTime);
//Compare
cutOffDateTime.compareTo(currentDateTime);
时区
is going in the right direction, but is not spot-on. The answer ignores the crucial issue of time zone。 Local…
类型故意丢失和忽略时区信息,这就是它们的目的。但我们很少希望丢失时区信息。
日期和时间的确定取决于时区。对于任何给定时刻,日期和时间在全球范围内可能会有所不同。在巴黎午夜过后几分钟是新的一天,而在蒙特利尔仍然是“昨天”。
Instant
class defines a moment on the timeline in UTC with a resolution of nanoseconds.
Instant now = Instant.now();
如果所需的截止日期是“明天下午 5 点”,则必须将时区指定为上下文。将 ZoneId
应用于 Instant
以获得 ZonedDateTime
.
ZoneId zoneId = ZoneId.of( "America/Montreal" );
ZonedDateTime zdt = ZonedDateTime.ofInstant( instant , zoneId );
ZonedDateTime zdtTomorrow = zdt.plusDays( 1 );
现在调整到下午 5 点。
LocalTime timeOfDayWhenDue = LocalTime.of( 5 , 0 );
ZonedDateTime zdtDeadline = zdtTomorrow.with( timeOfDayWhenDue );
您可以使用 isEqual
、isBefore
和 isAfter
方法进行比较。
ZonedDateTime now = ZonedDateTime.now( zoneId );
boolean overdue = now.isAfter( zdtDeadline );
您还可以将分区日期时间转换回 UTC。 ZonedDateTime
对象及其各自的 Instant
对象代表时间轴上的同一时刻(历史上的同一时刻),但从不同时区的角度来看(America/Montreal
与 UTC
).
Instant instantDeadline = zdtDeadline.toInstant();
Instant instantNow = now.toInstant();
boolean overdue = instantNow.isAfter( instantDeadline );
如果您想将截止日期告知印度的客户,请调整到另一个时区。日期时间值将代表时间轴上的同一时刻,但会显示一个对该客户有意义的 wall-clock time。
ZoneId zoneId_Kolkata = ZoneId.of( "Asia/Kolkata" );
ZonedDateTime zdtDeadline_Kolkata = zdtDeadline.withZoneSameInstant( zoneId_Kolkata );
如果您不指定时区,JVM 的当前默认时区将隐式、静默地应用。不好。一方面,隐含的假设使您的代码容易被误解,并使错误更难查明。更糟糕的是,默认值可以随时更改 ,当您部署到另一台计算机时,甚至在应用程序执行的任何时刻的运行时!最好始终指定 desired/expected 时区。顺便说一句,Locale
.
也是如此
目前我正在处理运输条件。在此我将 cut off time
反对公司,如 (05.00 PM)。
现在我想比较上面的时间和当前时间是在 cut off time
之前还是在 cut off time
之后?
我已经浏览了所有 link 我只能看到 date
的示例。随着时间的推移我找不到任何东西。
请让我知道或提供一些线索,以便我进行整理。
这是我目前尝试过的方法
String todayDate=LocalDate.now().toString("dd.MM.yyyy");
String s=todayDate+cutOffTime;//cutOffTime will get from DB
SimpleDateFormat simpleDateFormat=new SimpleDateFormat("dd.MM.yyyy HH:mm a");
LocalDate despatchDate=LocalDate.now();
try {
Date cutoffDate=simpleDateFormat.parse(s);
if (cutoffDate.after(Calendar.getInstance().getTime())){
despatchDate.plusDays(1);
}
} catch (ParseException e) {
e.printStackTrace();
}
Java 8 date/time api
LocalDateTime currentDateTime = LocalDateTime.now();
LocalDate currentDate = LocalDate.now();
String cutOff = "05:00 AM";
DateTimeFormatter timeParser = DateTimeFormatter.ofPattern("hh:mm a");
LocalTime cutOffTime = timeParser.parse(cutOff, LocalTime::from);
LocalDateTime cutOffDateTime = LocalDateTime.of(currentDate, cutOffTime);
//After
cutOffDateTime.isAfter(currentDateTime);
//Before
cutOffDateTime.isBefore(currentDateTime);
//Compare
cutOffDateTime.compareTo(currentDateTime);
时区
Local…
类型故意丢失和忽略时区信息,这就是它们的目的。但我们很少希望丢失时区信息。
日期和时间的确定取决于时区。对于任何给定时刻,日期和时间在全球范围内可能会有所不同。在巴黎午夜过后几分钟是新的一天,而在蒙特利尔仍然是“昨天”。
Instant
class defines a moment on the timeline in UTC with a resolution of nanoseconds.
Instant now = Instant.now();
如果所需的截止日期是“明天下午 5 点”,则必须将时区指定为上下文。将 ZoneId
应用于 Instant
以获得 ZonedDateTime
.
ZoneId zoneId = ZoneId.of( "America/Montreal" );
ZonedDateTime zdt = ZonedDateTime.ofInstant( instant , zoneId );
ZonedDateTime zdtTomorrow = zdt.plusDays( 1 );
现在调整到下午 5 点。
LocalTime timeOfDayWhenDue = LocalTime.of( 5 , 0 );
ZonedDateTime zdtDeadline = zdtTomorrow.with( timeOfDayWhenDue );
您可以使用 isEqual
、isBefore
和 isAfter
方法进行比较。
ZonedDateTime now = ZonedDateTime.now( zoneId );
boolean overdue = now.isAfter( zdtDeadline );
您还可以将分区日期时间转换回 UTC。 ZonedDateTime
对象及其各自的 Instant
对象代表时间轴上的同一时刻(历史上的同一时刻),但从不同时区的角度来看(America/Montreal
与 UTC
).
Instant instantDeadline = zdtDeadline.toInstant();
Instant instantNow = now.toInstant();
boolean overdue = instantNow.isAfter( instantDeadline );
如果您想将截止日期告知印度的客户,请调整到另一个时区。日期时间值将代表时间轴上的同一时刻,但会显示一个对该客户有意义的 wall-clock time。
ZoneId zoneId_Kolkata = ZoneId.of( "Asia/Kolkata" );
ZonedDateTime zdtDeadline_Kolkata = zdtDeadline.withZoneSameInstant( zoneId_Kolkata );
如果您不指定时区,JVM 的当前默认时区将隐式、静默地应用。不好。一方面,隐含的假设使您的代码容易被误解,并使错误更难查明。更糟糕的是,默认值可以随时更改 ,当您部署到另一台计算机时,甚至在应用程序执行的任何时刻的运行时!最好始终指定 desired/expected 时区。顺便说一句,Locale
.