Course – LS – All

Get started with Spring and Spring Boot, through the Learn Spring course:

>> CHECK OUT THE COURSE

1. Introduction

In this short tutorial, we’ll show how to get a Date without time in Java.

We’ll show how to do this before and after Java 8 since things have become a bit different after the release of the new time API in Java 8.

2. Before Java 8

Before Java 8, there wasn’t a direct way to get a Date without time unless we were using third party libraries like Joda-time.

This is because the Date class in Java is a representation of a specific instant in time, expressed in milliseconds. Therefore, this makes it impossible to ignore the time on a Date.

In the next sections, we’ll show some common workarounds to tackle this problem.

2.1. Using Calendar

One of the most common ways to get a Date without time is to use the Calendar class to set the time to zero. By doing this we get a clean date, with the time set at the start of the day.

Let’s see it in code:

public static Date getDateWithoutTimeUsingCalendar() {
    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.HOUR_OF_DAY, 0);
    calendar.set(Calendar.MINUTE, 0);
    calendar.set(Calendar.SECOND, 0);
    calendar.set(Calendar.MILLISECOND, 0);

    return calendar.getTime();
}

If we call this method we get a date like this:

Sat Jun 23 00:00:00 CEST 2018

As we can see, it returns a full date with the time set to zero, but we can’t ignore the time.

Also, to make sure that there is no time set in the Date returned, we can create the following test:

@Test
public void whenGettingDateWithoutTimeUsingCalendar_thenReturnDateWithoutTime() {
    Date dateWithoutTime = DateWithoutTime.getDateWithoutTimeUsingCalendar();

    Calendar calendar = Calendar.getInstance();
    calendar.setTime(dateWithoutTime);
    int day = calendar.get(Calendar.DAY_OF_MONTH);

    calendar.setTimeInMillis(dateWithoutTime.getTime() + MILLISECONDS_PER_DAY - 1);
    assertEquals(day, calendar.get(Calendar.DAY_OF_MONTH));

    calendar.setTimeInMillis(dateWithoutTime.getTime() + MILLISECONDS_PER_DAY);
    assertNotEquals(day, calendar.get(Calendar.DAY_OF_MONTH));
}

As we can see, when we add to the date the milliseconds of a day minus one we still get the same day, but when we add a full day we get the next day.

2.2. Using a Formatter

Another way to do this is by formatting a Date into a String without the time and then convert that String back to a Date. Since the String was formatted without the time the Date converted will have the time set to zero.

Let’s implement it to see how it works:

public static Date getDateWithoutTimeUsingFormat() 
  throws ParseException {
    SimpleDateFormat formatter = new SimpleDateFormat(
      "dd/MM/yyyy");
    return formatter.parse(formatter.format(new Date()));
}

This implementation returns the same as the method shown in the previous section:

Sat Jun 23 00:00:00 CEST 2018

Again, we can use a test like we did before to make sure there is no time in the Date returned.

3. Using Java 8

With the release of the new time API in Java 8, there is an easier way to get a date without the time. One of the new features that this new API has brought is that there are several classes to work with dates with or without time, or even to work only with time.

For the sake of this article, we’ll focus on the class LocalDate, which represents exactly what we need, a date without the time.

Let’s see it with this example:

public static LocalDate getLocalDate() {
    return LocalDate.now();
}

This method returns a LocalDate object with this date representation:

2018-06-23

As we can see, it returns just a date, the time is completely ignored.

Again, let’s test it as before to make sure that this method works as expected:

@Test
public void whenGettingLocalDate_thenReturnDateWithoutTime() {
    LocalDate localDate = DateWithoutTime.getLocalDate();

    long millisLocalDate = localDate
      .atStartOfDay()
      .toInstant(OffsetDateTime
        .now()
        .getOffset())
      .toEpochMilli();

    Calendar calendar = Calendar.getInstance();

    calendar.setTimeInMillis(
      millisLocalDate + MILLISECONDS_PER_DAY - 1);
    assertEquals(
      localDate.getDayOfMonth(), 
      calendar.get(Calendar.DAY_OF_MONTH));

    calendar.setTimeInMillis(millisLocalDate + MILLISECONDS_PER_DAY);
    assertNotEquals(
      localDate.getDayOfMonth(), 
      calendar.get(Calendar.DAY_OF_MONTH));
}

4. Conclusion

In this article, we’ve shown how to get a Date without time in Java. We first showed how to do it prior to Java 8 and also using Java 8.

As we could see in the examples implemented in the article, using Java 8 should be always the preferred option, since it has a specific representation to work with dates without time.

As always, the full source code of the examples is available over on GitHub.

Course – LS – All

Get started with Spring and Spring Boot, through the Learn Spring course:

>> CHECK OUT THE COURSE
res – REST with Spring (eBook) (everywhere)
Comments are closed on this article!