如何计算今天上午 00:00:00 的日期?

发布于 2024-09-07 05:38:09 字数 308 浏览 3 评论 0原文

我需要计算今天开始的 java.util.Date (今天的上午 00:00:00)。有人知道比重置 java.util.Calendar 字段更好的事情吗:

Calendar cal = Calendar.getInstance();
cal.set(Calendar.AM_PM, Calendar.AM);
cal.set(Calendar.HOUR, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);

I need to calculate a java.util.Date for a beginning of a today day (00:00:00 a.m. of a today day). Does someone know something better than resetting fields of java.util.Calendar:

Calendar cal = Calendar.getInstance();
cal.set(Calendar.AM_PM, Calendar.AM);
cal.set(Calendar.HOUR, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

永不分离 2024-09-14 05:38:09

如果您不关心时区,那么您的解决方案就可以。否则值得看看 JodaTime。

如果您最终决定切换到 JodaTime,那么您可以使用 DateMidnight 类应该在您的情况下使用。

If you don't bother about time zones then your solution is ok. Otherwise it's worth to look at JodaTime.

If you eventually decide to switch to JodaTime, then you can use DateMidnight class which is supposed to be used in your situation.

り繁华旳梦境 2024-09-14 05:38:09

以下代码将返回当前日期的日历对象,时间为 00:00:00

Calendar current = Calendar.getInstance();
current.set(current.get(Calendar.YEAR),current.get(Calendar.MONTH),current.get(Calendar.DATE),0,0,0);

它不会考虑时区值,并且与您的代码几乎相同。唯一的区别是它是在一行中将所有重置为 0。

The following code will return the current date's calendar object with time as 00:00:00

Calendar current = Calendar.getInstance();
current.set(current.get(Calendar.YEAR),current.get(Calendar.MONTH),current.get(Calendar.DATE),0,0,0);

It will not consider the timezone values and is almost same as your code. Only difference is that it is done all resets to 0 in single line.

心舞飞扬 2024-09-14 05:38:09

这个解决方案可以更好,因为它没有使用java的重元素Calender

public class DateTest {
    public static void main(String[] args) {
       SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
    long time = dateFormat.parse(dateFormat.format(new Date())).getTime();
    System.out.println("todays start date : " + new Date(time));
    }
}

This solution can be better as it does not make use of java's heavy element Calender

public class DateTest {
    public static void main(String[] args) {
       SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
    long time = dateFormat.parse(dateFormat.format(new Date())).getTime();
    System.out.println("todays start date : " + new Date(time));
    }
}
菊凝晚露 2024-09-14 05:38:09

我在同一条船上,你提供的就是我的做法。我确实将它放在 DateUtil.stripToMidnight(...) 函数中...

但是请记住在执行所有这些操作时考虑时区,因为此处的上午 0:00 在其他部分中不会相同世界的。

JodaTime 中可能有一种方法可以做到这一点,但我不知道。

I'm in the same boat, and what you have provided is how I do it. I do have it in a DateUtil.stripToMidnight(...) function...

But just remember to consider TimeZones when doing all this, as 0:00am here, will not be the same in another part of the world.

There might be a way to do this in JodaTime, but I don't know of it.

各自安好 2024-09-14 05:38:09

使用 date4j 库:

DateTime start = dt.getStartOfDay();

With the date4j library :

DateTime start = dt.getStartOfDay();
白芷 2024-09-14 05:38:09

无需额外库的另一种选择:

import java.sql.Date;

public class DateTest {
    public static void main(String[] args) {
        long MS_PER_DAY = 24L * 60 * 60 * 1000;
        long msWithoutTime = (System.currentTimeMillis() / MS_PER_DAY) * MS_PER_DAY;
        Date date = new Date( msWithoutTime );
        System.out.println( date.toGMTString());
    }
}

Another option without additional libraries:

import java.sql.Date;

public class DateTest {
    public static void main(String[] args) {
        long MS_PER_DAY = 24L * 60 * 60 * 1000;
        long msWithoutTime = (System.currentTimeMillis() / MS_PER_DAY) * MS_PER_DAY;
        Date date = new Date( msWithoutTime );
        System.out.println( date.toGMTString());
    }
}
智商已欠费 2024-09-14 05:38:09

从 java8 开始,假设您不关心时区,可以按如下方式完成

LocalDateTime localDateTime = LocalDateTime.of(LocalDate.now(), LocalTime.MIDNIGHT);
Date midnight = Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());

As of java8, it can be done as follow assuming you don't care about timezone

LocalDateTime localDateTime = LocalDateTime.of(LocalDate.now(), LocalTime.MIDNIGHT);
Date midnight = Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文