如何使用 Java 中的 Joda 日期/时间库获取“今天”的日期/时间范围?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6525395/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 06:39:07  来源:igfitidea点击:

How do you get the date/time range for "today" using the Joda date/time library in Java?

javadatetimejodatime

提问by Morris

Assuming this is how you get the current time in Joda time:

假设这是您在Joda 时间中获取当前时间的方式

DateTime now = new DateTime();

How do you calculate values for the variables dateTimeAtStartOfTodayand dateTimeAtEndOfToday?

你如何计算变量dateTimeAtStartOfToday和 的值dateTimeAtEndOfToday

What I'm trying to do is generate some SQL to do a lookup of all transactions that have occurred between the startOfTodayand endOfToday.

我想要做的是生成一些 SQL 来查找在startOfToday和之间发生的所有事务endOfToday

采纳答案by Jon Skeet

I would use:

我会用:

LocalDate today = now.toLocalDate();
LocalDate tomorrow = today.plusDays(1);

DateTime startOfToday = today.toDateTimeAtStartOfDay(now.getZone());
DateTime startOfTomorrow = tomorrow.toDateTimeAtStartOfDay(now.getZone());

Then check if startOfToday <= time < startOfTomorrowfor any particular time.

然后检查是否startOfToday <= time < startOfTomorrow有任何特定时间。

Of course, it partly depends on exactly what's stored in the database - and what time zone you're interested in.

当然,这部分取决于数据库中存储的内容 - 以及您感兴趣的时区。

回答by Eric Normand

import org.joda.time.DateTime;
import org.joda.time.DateTimeMidnight;

DateTime dateTimeAtStartOfToday = new DateTime(new DateTimeMidnight());  
DateTime dateTimeAtEndOfToday = new DateTime((new DateTimeMidnight()).plusDays(1));

回答by Bob Kuhar

This works...

这工作...

DateTime dt = new DateTime();
DateMidnight dtStartDate = dt.toDateMidnight();
DateMidnight dtEndDate = dt.plusDays( 1 ).toDateMidnight();
System.out.println( dt + "\n" + dtStartDate + "\n" + dtEndDate );

...but as far as the SQL, I tend to use BETWEEN as the where clause rather than do the > and <= stuff

...但就 SQL 而言,我倾向于使用 BETWEEN 作为 where 子句,而不是使用 > 和 <= 的东西

回答by Bob Kuhar

This works better, it turns out DateTime has a method called toInterval which does this exact thing (figures out midnight to midnight). In my tests, it appears to have no problem with DST transitions.

这效果更好,事实证明 DateTime 有一个名为 toInterval 的方法,它可以完成这个确切的事情(计算午夜到午夜)。在我的测试中,DST 转换似乎没有问题。

DateTime now = new DateTime();
DateTime startOfToday = now.toDateMidnight().toInterval().getStart();
DateTime endOfToday = now.toDateMidnight().toInterval().getEnd();
System.out.println( "\n" + now + "\n" + startOfToday + "\n" + endOfToday + "\n" );

JODA looks to be very well thought out.

JODA 看起来是经过深思熟虑的。

回答by Prakash Nadar

if((sinceDate.getDayOfYear() == now.getDayOfYear())  && (sinceDate.year() == now.year()))
   //yep, do something today;

works for me.

为我工作。

回答by hardysim

As a Kotlin extension function it looks like this:

作为 Kotlin 扩展函数,它看起来像这样:

fun DateTime.isOnSameDay(timeOnDayToCheck: DateTime) =
  timeOnDayToCheck.toLocalDate().toInterval(this.zone).contains(this)

This does not contain times equaling the endof the day (start is included) so maybe add an "or"-case with interval.getEnd().isEqual(this)).

这不包含等于一天结束的时间(包括开始),因此可以添加一个“或”案例interval.getEnd().isEqual(this))