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

当前回答

记住,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…

其他回答

JodaTime最简单的方法

日期日期=日期日期。

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.

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)

JDK8 - Java时间模块方式:

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

也工作:

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

记住,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…