在我的代码中,我需要找到今天发生的所有事情。因此,我需要将今天凌晨00:00(今天凌晨午夜)到中午12:00(今晚午夜)的日期进行比较。

我知道……

Date today = new Date(); 

... 我现在很生气。和…

Date beginning = new Date(0);

…1970年1月1日是零。但是有什么简单的方法可以让你今天和明天的时间都为零呢?


更新

我做到了,但肯定有更简单的方法吧?

Calendar calStart = new GregorianCalendar();
calStart.setTime(new Date());
calStart.set(Calendar.HOUR_OF_DAY, 0);
calStart.set(Calendar.MINUTE, 0);
calStart.set(Calendar.SECOND, 0);
calStart.set(Calendar.MILLISECOND, 0);
Date midnightYesterday = calStart.getTime();
            
Calendar calEnd = new GregorianCalendar();
calEnd.setTime(new Date());
calEnd.set(Calendar.DAY_OF_YEAR, calEnd.get(Calendar.DAY_OF_YEAR)+1);
calEnd.set(Calendar.HOUR_OF_DAY, 0);
calEnd.set(Calendar.MINUTE, 0);
calEnd.set(Calendar.SECOND, 0);
calEnd.set(Calendar.MILLISECOND, 0);
Date midnightTonight = calEnd.getTime();

当前回答

为了完整起见,如果您正在使用Java 8,您还可以使用Instant类的truncatedTo方法来获取UTC时间的午夜。

Instant.now().truncatedTo(ChronoUnit.DAYS);

用Javadoc编写

例如,使用MINUTES单位进行截断将四舍五入到最近的分钟,将秒和纳秒设置为零。

希望能有所帮助。

其他回答

我的做法和这里的其他人都不一样。我刚接触Java,所以我的解决方案可能很差。

Date now = new Date();
Date midnightToday = new Date(now.getYear(), now.getMonth(), now.getDate());

我不确定这是否有效,但无论如何,我很感激任何关于这个解决方案的反馈。

我对上面的陈述感到困惑,你可以通过调用:

c.add(Calendar.DAY_OF_MONTH, 1);

如果你在这个月的第几天加上1是31天,你不是得到了这个月的第32天吗?

为什么Java中的时间/日期不是全部基于UTC ?我认为时区应该只在与I /o一起使用时才需要,但在内部应该总是在UTC中使用。然而,这些类似乎包含了Timezone信息,这不仅浪费时间,而且容易产生编码错误。

从JodaTime 2.3开始,toDateMidnight()已弃用。

从2.2升级到2.3

    Deprecations since 2.2
    ----------------------
    - DateMidnight [#41]
     This class is flawed in concept
     The time of midnight occasionally does not occur in some time-zones
     This is a result of a daylight savings time from 00:00 to 01:00
     DateMidnight is essentially a DateTime with a time locked to midnight
     Such a concept is more generally a poor one to use, given LocalDate
     Replace DateMidnight with LocalDate
     Or replace it with DateTime, perhaps using the withTimeAtStartOfDay() method

下面是一个没有toDateMidnight()方法的示例代码。

Code

DateTime todayAtMidnight = new DateTime().withTimeAtStartOfDay();
System.out.println(todayAtMidnight.toString("yyyy-MM-dd HH:mm:ss"));

输出(根据您所在的时区可能有所不同)

2013-09-28 00:00:00

记住,Date不是用来表示日期的(!)。为了表示日期,你需要一个日历。这样的:

Calendar c = new GregorianCalendar();

将创建一个日历实例,表示当前时区的当前日期。现在需要做的是截断日以下的每个字段(小时、分钟、秒和毫秒),将其设置为0。今天是午夜。

现在要得到第二天的午夜,你需要添加一天:

c.add(Calendar.DAY_OF_MONTH, 1);

请注意,加上86400秒或24小时是不正确的,因为在此期间可能会发生夏季时间。

更新:然而,我最喜欢的处理这个问题的方法是使用来自Commons Lang的DateUtils类:

Date start = DateUtils.truncate(new Date(), Calendar.DAY_OF_MONTH))
Date end = DateUtils.addDays(start, 1);

它在幕后使用Calendar…

Date now= new Date();
// Today midnight
Date todayMidnight = new Date(endTime.getTime() -endTime.getTime()%DateUtils.MILLIS_PER_DAY);

// tomorrow midnight
Date tomorrowMidnight = new Date(endTime.getTime() -endTime.getTime()%DateUtils.MILLIS_PER_DAY + DateUtils.MILLIS_PER_DAY);

JDK8 - Java时间模块方式:

LocalDateTime todayMidnight = LocalDate.now().atStartOfDay();

也工作:

LocalDateTime todayMidnight = LocalDateTime.now().with(LocalTime.MIDNIGHT);