如何比较java中的三个日期/时间值?
目前我使用Date对象
我的意思是: 日期1 - 今天,晚上10:00 日期2 - 明天,上午5:00 日期3 - 当前时间date3和date2之间的date3,尊重日期和时间?
date1.compareTo(date3) * date2.compareTo(date3) > 0
和
date1.after(date3) && date2.before(date3)
无效。
我在Android App中使用此代码,如果我将时间设置为11.30 AM,则在上述条件下仍会返回true。如果我使用时间对象和第二种方法,它不会识别我的时间跨度是2天。
有什么想法吗?
编辑:为了使其准确,这是我当前的代码。应用程序就像一个闹钟。
// Current Date/Time
Date now = Calendar.getInstance().getTime();
// Time when user goes to bed (current day)
Date sleep = new Date(now.getYear(), now.getMonth(), now.getDate(), Shours, Sminutes);
// Time when user wakes up (next day)
// Get Next Day's Date and set Time
Calendar wk = Calendar.getInstance();
wk.setTime(sleep);
wk.set(Calendar.HOUR_OF_DAY, Whours);
wk.set(Calendar.MINUTE, Wminutes);
// tomorrow
wk.add(Calendar.DATE, 1);
// and convert to date
Date wake = wk.getTime();
// Compare
if(now.after(sleep) && now.before(wake)) {
Log.d("uSleep", "Debug: Night time");
}
else {
Log.d("uSleep", "Debug: Day Time");
}
也许它仍然难以理解。图片你晚上10点睡觉,你早上5点起床。现在,如何通过比较你的"去睡觉" -time和#34;起床" -time到当前时间来了解你是否正在睡觉。我需要使用"明天"为了你的"起床" -time,否则java似乎在同一天的所有时间进行比较,这是不可能的。
答案 0 :(得分:1)
Interval.of(
start ,
stop
).contains(
ZonedDateTime.now( ZoneId.of( "America/Montreal" ) )
.toInstant()
)
这个问题令人困惑,但似乎是......
如何确定特定时刻是否在一段时间内发生?
我的意思是:Date1 - 今天,10:00 PM Date2 - 明天,5:00 AM Date3 - 当前时间
时区对于确定“今天”和“明天”至关重要。对于任何给定的时刻,日期在全球范围内因地区而异。例如,在Paris France午夜后的几分钟是新的一天,而Montréal Québec中仍然是“昨天”。
以continent/region
的格式指定proper time zone name,例如America/Montreal
,Africa/Casablanca
或Pacific/Auckland
。切勿使用诸如EST
或IST
之类的3-4字母缩写,因为它们不是真正的时区,不是标准化的,甚至不是唯一的(!)。
ZoneId z = ZoneId.of( "America/Montreal" );
ZonedDateTime now = ZonedDateTime.now( z );
要与其他时间获取相同的日期,请摘录LocalDate
。 LocalDate
类表示没有时间且没有时区的仅日期值。使用LocalTime
指定所需的时间。与ZoneId
结合使用即可获得ZonedDateTime
。
LocalDate today = now.toLocalDate() ;
LocalTime tenPm = LocalTime.of( 22 , 0 ) ; // 10 PM is 22:00.
ZonedDateTime tenPmToday = ZonedDateTime.of( today , tenPm , z ) ;
为了明天,请在今天的日期添加一天。
LocalDate tomorrow = ld.plusDays( 1 ) ;
LocalTime fiveAm = LocalTime.of( 5 , 0 ) ;
ZonedDateTime fiveAmTomorrow = ZonedDateTime.of( tomorrow ,fiveAm , z ) ;
要进行比较,请调用isBefore
,isEqual
和isAfter
方法。
Boolean contains = ( ! now.isBefore( tenPmToday ) ) && now.isBefore( fiveAmTomorrow ) ;
当然,现在总是在明天之前,所以我不确定你的意图。
org.threeten.extra.Interval
您可以从下面列出的Interval
项目中找到对此工作有用的ThreeTen-Extra课程。此类存储一对Instant
对象,并具有一些方便的比较方法,如contains
。
Instant
类代表UTC中时间轴上的一个时刻,分辨率为nanoseconds(小数部分最多九(9)位)。
Interval interval = Interval.of( tenPmToday.toInstant() , fiveAmTomorrow.toInstant() ) ;
Boolean contains = interval.contains( now.toInstant() ) ;
java.time框架内置于Java 8及更高版本中。这些类取代了麻烦的旧legacy日期时间类,例如java.util.Date
,Calendar
和& SimpleDateFormat
现在位于Joda-Time的maintenance mode项目建议迁移到java.time类。
要了解详情,请参阅Oracle Tutorial。并搜索Stack Overflow以获取许多示例和解释。规范是JSR 310。
从哪里获取java.time类?
ThreeTen-Extra项目使用其他类扩展java.time。该项目是未来可能添加到java.time的试验场。您可以在此处找到一些有用的课程,例如Interval
,YearWeek
,YearQuarter
和more。
更新: Joda-Time项目现在位于maintenance mode,团队建议迁移到java.time类。
最好的方法是使用Joda-Time库,而不是java.util.Date/.Calendar类,这些类是众所周知的麻烦,令人困惑和有缺陷的。
在Joda-Time中,您可以通过三种方式表示时间跨度:间隔,周期和持续时间。在这种情况下,我们需要Interval
,由时间轴中的一对特定点定义,采用半开[)
方法,其中开头是包含的,结尾是独占的。
这对特定点以及现在的当前时刻都由DateTime
类表示。与java.util.Date不同,DateTime知道自己指定的时区。如果未指定,将应用JVM的当前默认时区。因此通常更好地指定。
使用Joda-Time 2.5的一些示例代码。
DateTimeZone zone = DateTimeZone.forID( "America/Montreal" );
DateTime now = DateTime.now( zone );
DateTime bedtime = now.withTime( 22, 0, 0, 0 ); // Today’s bedtime. May be past, future, or this very moment now.
DateTime risetime = bedtime.plusHours( 7 ); // 05:00 next morning.
Interval sleepInterval = new Interval( bedtime, risetime );
boolean asleep = sleepInterval.contains( now ); // Half-Open "[)" comparison, beginning is inclusive, ending exclusive.