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?
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.
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.
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".
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.
LocalDate ld =
myUtilDate.toInstant()
.atZone( ZoneId.of( "America/Montreal" ) )
.toLocalDate();
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
truncatedTo more directly address the question:
java.util.Date
to java.time.Instant
.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();
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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With