Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to trim minutes and hours and seconds from Date object?

Tags:

java

date

I need to make map where Dates are keys. 2 date objects are equals if they have the same value of getTime() method.

I'm interested only in year, month and day. How can I trim unnecessary hours and minutes to get 'clear' dates?

like image 657
Roman Avatar asked May 05 '10 17:05

Roman


People also ask

How do I remove the time from a date object?

Use the toDateString() method to remove the time from a date, e.g. new Date(date. toDateString()) . The method returns only the date portion of a Date object, so passing the result to the Date() constructor would remove the time from the date.

How to cut time from date in js?

Given a Date Object and the task is to remove the time portion of the object using JavaScript. split() method: This method is used to split a string into an array of substrings, and returns the new array.

How do I remove T and Z from timestamp?

To remove the T and Z characters from an ISO date in JavaScript, we first need to have the date in ISO format. If you don't have it already, you can convert it to ISO using the toISOString function on a Date object. This will get rid of both T and Z, resulting in "2022-06-22 07:54:52.657".

How do you truncate a date in Java?

Use DateUtils. truncate() to throw out all fields less significant than the specified field. When a Date is truncated to the Calendar. MONTH field, DateUtils.


1 Answers

tl;dr

LocalDate ld =
    myUtilDate.toInstant()
              .atZone( ZoneId.of( "America/Montreal" ) )
              .toLocalDate();

Details

The Question and other Answers use outmoded old date-time classes that have proven to be poorly-designed, confusing, and troublesome. Now legacy, supplanted by the java.time classes.

Instant truncated

To more directly address the question:

  1. Convert to java.time, from java.util.Date to java.time.Instant.
  2. Truncate to the date.

Convert via new methods added to the old classes.

Instant instant = myUtilDate.toInstant();

The truncation feature is built into the Instant class. The Instant class represents a moment on the timeline in UTC with a resolution of nanoseconds (up to nine (9) digits of a decimal fraction).

Instant instantTruncated = instant.truncatedTo( ChronoUnit.DAYS );

ZonedDateTime & LocalDate

But the approach above has issues. Both java.util.Date and Instant represent a moment on the timeline in UTC rather than an particular time zone. So if you drop the time-of-day, or set it to 00:00:00, you are getting a date that only makes sense in UTC. If you meant the date for Auckland NZ or Montréal Québec, you may have the wrong date.

So a better approach is to apply your desired/expected time zone to the Instant to get a ZonedDateTime.

Another problem is that we are inappropriately using a date-time object to represent a date-only value. Instead we should use a date-only class. From the ZonedDateTime we should extract a LocalDate if all you want is the date-only.

The LocalDate class represents a date-only value without time-of-day and without time zone.

ZoneId z = ZoneId.of( "America/Montreal" );
ZonedDateTime zdt = instant.atZone( z );
LocalDate ld = zdt.toLocalDate();

About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

The Joda-Time project, now in maintenance mode, advises migration to java.time.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

Where to obtain the java.time classes?

  • Java SE 8 and SE 9 and later
    • Built-in.
    • Part of the standard Java API with a bundled implementation.
    • Java 9 adds some minor features and fixes.
  • Java SE 6 and SE 7
    • Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
  • Android
    • The ThreeTenABP project adapts ThreeTen-Backport (mentioned above) for Android specifically.
    • See How to use….

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.

like image 70
Basil Bourque Avatar answered Oct 29 '22 15:10

Basil Bourque