如何获取“今天”的日期/时间范围在Java中使用Joda日期/时间库? [英] How do you get the date/time range for "today" using the Joda date/time library in Java?

查看:338
本文介绍了如何获取“今天”的日期/时间范围在Java中使用Joda日期/时间库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设这是您如何获得 Joda时间的当前时间:

  DateTime now = new DateTime(); 

如何计算变量的值 dateTimeAtStartOfToday dateTimeAtEndOfToday



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

解决方案

我会使用:

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

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

然后检查 startOfToday< = time< startOfTomorrow 在任何特定的时间。



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


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

DateTime now = new DateTime();

How do you calculate values for the variables dateTimeAtStartOfToday and dateTimeAtEndOfToday?

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

解决方案

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 < startOfTomorrow for any particular time.

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

这篇关于如何获取“今天”的日期/时间范围在Java中使用Joda日期/时间库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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