Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a Java Date object of midnight today and midnight tomorrow?

Tags:

java

date

People also ask

How do you create a date object in Java?

You can create a Date object using the Date() constructor of java. util. Date constructor as shown in the following example. The object created using this constructor represents the current time.

How can I get tomorrow date in Java?

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

How can I get yesterday date in Java Util date?

There is no direct function to get yesterday's date. To get yesterday's date, you need to use Calendar by subtracting -1 .


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 and 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);

Joda-Time

If you're using a JDK < 8, I recommend Joda Time, because the API is really nice:

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

Since version 2.3 of Joda Time DateMidnight is deprecated, so use this:

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

Pass a time zone if you don't want the JVM’s current default time zone.

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

For sake of completeness, if you are using Java 8 you can also use the truncatedTo method of the Instant class to get midnight in UTC.

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

As written in the Javadoc

For example, truncating with the MINUTES unit will round down to the nearest minute, setting the seconds and nanoseconds to zero.

Hope it helps.


The easiest way to find a midnight:

Long time = new Date().getTime();
Date date = new Date(time - time % (24 * 60 * 60 * 1000));

Next day:

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

Remember, Date is not used to represent dates (!). To represent date you need a calendar. This:

Calendar c = new GregorianCalendar();

will create a Calendar instance representing present date in your current time zone. Now what you need is to truncate every field below day (hour, minute, second and millisecond) by setting it to 0. You now have a midnight today.

Now to get midnight next day, you need to add one day:

c.add(Calendar.DAY_OF_MONTH, 1);

Note that adding 86400 seconds or 24 hours is incorrect due to summer time that might occur in the meantime.

UPDATE: However my favourite way to deal with this problem is to use DateUtils class from Commons Lang:

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

It uses Calendar behind the scenes...