在我的代码中,我需要找到今天发生的所有事情。因此,我需要将今天凌晨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();

当前回答

因为一天是24 * 60 * 60 * 1000毫秒, 这一天的午夜可以计算为……

long now = System.currentTimeMillis();
long delta = now % 24 * 60 * 60 * 1000;
long midnight = now - delta;
Date midnightDate = new Date(midnight);`

其他回答

我的做法和这里的其他人都不一样。我刚接触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信息,这不仅浪费时间,而且容易产生编码错误。

我知道这是一个很老的帖子。我想在这里分享我的知识!

对于日期今天午夜与准确的时区,您可以使用以下

public static Date getCurrentDateWithMidnightTS(){

    return new Date(System.currentTimeMillis() - (System.currentTimeMillis()%(1000*60*60*24)) - (1000*60 * 330));
}

其中(1000*60 * 330)被减去,即实际上与时区有关,例如印度时区,即加尔各答与实际时差+5:30小时。减去换算成毫秒的时间。

所以根据你的需要改变最后一个减数。我正在创建一个产品,即只基于印度,所以只是使用特定的时间戳。

java.util.Calendar

// today    
Calendar date = new GregorianCalendar();
// reset hour, minutes, seconds and millis
date.set(Calendar.HOUR_OF_DAY, 0);
date.set(Calendar.MINUTE, 0);
date.set(Calendar.SECOND, 0);
date.set(Calendar.MILLISECOND, 0);

// next day
date.add(Calendar.DAY_OF_MONTH, 1);

JDK 8 - java.time.LocalTime和java.time.LocalDate

LocalTime midnight = LocalTime.MIDNIGHT;
LocalDate today = LocalDate.now(ZoneId.of("Europe/Berlin"));
LocalDateTime todayMidnight = LocalDateTime.of(today, midnight);
LocalDateTime tomorrowMidnight = todayMidnight.plusDays(1);

乔达时间

如果你使用的是小于8的JDK,我推荐Joda Time,因为这个API真的很棒:

DateTime date = new DateTime().toDateMidnight().toDateTime();
DateTime tomorrow = date.plusDays(1);

因为2.3版本的Joda Time DateMidnight已弃用,所以使用这个:

DateTime today = new DateTime().withTimeAtStartOfDay();
DateTime tomorrow = today.plusDays(1).withTimeAtStartOfDay();

如果不希望使用JVM当前的默认时区,则传递一个时区。

DateTimeZone timeZone = DateTimeZone.forID("America/Montreal");
DateTime today = new DateTime(timeZone).withTimeAtStartOfDay(); // Pass time zone to constructor.

java.time

如果您使用的是Java 8及以上版本,您可以尝试Java。时间包(教程):

LocalDate tomorrow = LocalDate.now().plusDays(1);
Date endDate = Date.from(tomorrow.atStartOfDay(ZoneId.systemDefault()).toInstant());

Apache Commons Lang

DateUtils.isSameDay(date1, date2)

http://commons.apache.org/proper/commons lang/javadocs/api - 2.6 - / - org/apache/commons/lang/time/dateutils.html # isSameDay (java.util.Date java.util.Date)