Explain Codes LogoExplain Codes Logo

How do I get a Date without time in Java?

java
date-formatting
java-time
localdate
Nikita BarsukovbyNikita Barsukov·Oct 7, 2024
TLDR

Need a date without the time? Use LocalDate:

LocalDate date = LocalDate.now(); // "Oops, where did the time go?!"

LocalDate, part of the java.time package, is your trustworthy companion for date-only operations in Java.

Introduction to date without time in Java

Before dipping toes in complicated aspects, let's go for a simple swing with Java's inbuilt LocalDate class from the java.time package. LocalDate is a straightforward, efficient route towards getting a date without any time components involved.

Legacy classes vs java.time

In prehistoric Java times, java.util.Date and Calendar were used to handle date-time operations. However, these are mutable classes and can lead to unpredictable and error-prone results — generating a medley of headaches for developers.

Using the right time zone

To precisely handle dates across various regions, define the appropriate time zone when obtaining a LocalDate:

LocalDate today = LocalDate.now(ZoneId.of("UTC")); // "Always on 'time', even without time!"

This ensures the correct date for the specified time zone, eliminating potential discrepancies due to discrepancies in regional time.

Database handling

Interacting with databases demands precision. For date-only operations, stick to java.time or Joda Time. They make your transactions clearer and shield you from time zone shifts and daylight saving quirks.

Strengthening performance

When efficiency tops your priority list, dodge creating unnecessary transient objects. For instance, trim time fields from java.util.Date objects directly:

java.util.Date date = DateUtils.truncate(new java.util.Date(), Calendar.DATE); // "Snip, snip! No more time."

The above operation using DateUtils from Apache Commons returns a truncated java.util.Date object, simultaneously minimizing object creation.

Backward compatibility

Running on Java 6 or 7? Don't worry! Use the ThreeTen-Backport library, which recreates java.time functionality for these versions. For Android developers, the ThreeTenABP project is your lifesaver.

Using SimpleDateFormat when necessary

Legacy systems or certain use cases might demand the use of SimpleDateFormat. Keep in mind, though, these operations aren't as efficient and come with their own set of foibles.

SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy"); // "Who needs time, anyway?" java.util.Date dateWithoutTime = sdf.parse("25/12/2023");

Extra clarity for your code

Make your intentions clear with action-oriented types. If something is supposed to act as a date without time, ensure you use LocalDate. This makes your code easier to read and maintain, while also reducing the risk of bugs.

Expanding the usage of java.time classes

If the standard java.time classes don't cover your needs, ThreeTen-Extra project might. It provides additional classes to expand the utility and functionality of the java.time framework.