how to create a Java Date object of midnight today

2019-01-03 22:07发布

In my code I need to find all my things that happened today. So I need to compare against dates from today at 00:00am (midnight early this morning) to 12:00pm (midnight tonight).

I know ...

Date today = new Date(); 

... gets me right now. And ...

Date beginning = new Date(0);

... gets me zero time on Jan 1, 1970. But what's an easy way to get zero time today and zero time tomorrow?

UPDATE; I did this, but surely there's an easier way?

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 date
19条回答
何必那么认真
2楼-- · 2019-01-03 22:33

The simplest way with JodaTime

DateMidnight date = DateMidnight.now();

查看更多
爷、活的狠高调
3楼-- · 2019-01-03 22:35

Old fashioned way..

private static Date getDateWithMidnight(){
    long dateInMillis = new Date().getTime();
    return new Date(dateInMillis - dateInMillis%(1000*60*60*24) - TimeZone.getDefault().getOffset(dateInMillis));
}
查看更多
何必那么认真
4楼-- · 2019-01-03 22:36

I did this differently than everyone else here did. I'm new to Java, so maybe my solution is poor.

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

I'm not sure this works yet, but either way, I'd appreciate any feedback on this solution.

I'm confused by the statement above that you can calculate tomorrow by calling:

c.add(Calendar.DAY_OF_MONTH, 1);

If you add 1 to the day of the month and it's the 31st day, don't you get the 32nd day of the month?

Why are times/dates not all based on UTC in Java? I would think Timezones should only be needed when used with i/o, but internally should always be used in UTC. However, the classes seem to include Timezone info which seems not only wasteful, but prone to coding errors.

查看更多
SAY GOODBYE
5楼-- · 2019-01-03 22:36

I know this is very old post. I thought to share my knowledge here !

For the date Mid night today with exact Time zone you can use following

public static Date getCurrentDateWithMidnightTS(){

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

Where (1000*60 * 330) is being subtracted i.e. actually related to time zone for example Indian time zone i.e. kolkata differs +5:30hrs from actual . So subtracting that with converting into milliseconds.

So change last substracted number according to you. I m creating a product i.e. only based in India So just used specific timestamp.

查看更多
霸刀☆藐视天下
6楼-- · 2019-01-03 22:37
Date todayMidnightUTC = java.sql.Date.valueOf(LocalDate.now());
Date tomorrowMidnightUTC = java.sql.Date.valueOf(LocalDate.now().plusDays(1));
Date anyMidnightLocal = java.sql.Date.valueOf(LocalDate.from(dateTime.toInstant().atZone(ZoneId.systemDefault())));

But beware that java.sql.Date.toInstant() always throws UnsupportedOperationException.

Via LocalDate to java.util.Date and vice versa simpliest conversion?

查看更多
时光不老,我们不散
7楼-- · 2019-01-03 22:41

This appears to be an option:

DateFormat justDay = new SimpleDateFormat("yyyyMMdd");
Date thisMorningMidnight = justDay.parse(justDay.format(new Date()));

to add a day to it, either

Date tomorrow = new Date(thisMorningMidnight.getTime() + 24 * 60 * 60 * 1000);

or

Calendar c = Calendar.getInstance();
c.setTime(thisMorningMidnight);
c.add(Calendar.DATE, 1);
Date tomorrowFromCalendar = c.getTime();

I have a hunch the latter is preferred in case of something weird like daylight savings causing adding 24 hours to not be enough (see https://stackoverflow.com/a/4336131/32453 and its other answers).

查看更多
登录 后发表回答