Mdhar9e
Mdhar9e

Reputation: 1376

substract one millisecond to the given Date in Java

i have a Date field expiryDate with value Thu Nov 21 00:00:00 IST 2019 But i am trying to get the endDate time to the before to the day by removing a millisecond from the time as Thu Nov 20 23:59:59 IST 2019 do we have any methods to remove a millisecond from the given Date.

Upvotes: 1

Views: 1103

Answers (2)

Basil Bourque
Basil Bourque

Reputation: 340250

java.time

Avoid the terrible date-time classes that are now legacy as of JSR 310. Now supplanted by the modern java.time classes.

Table of date-time types in Java, both modern and legacy

You can easily convert back and forth. Call new conversion methods addd to the old classes.

Instant instant = myJavaUtilDate.toInstant() ;

And back again.

java.util.Date myJavaUtilDate = Date.from( instant ) ;

Subtract a millisecond.

Instant oneMilliEarlier = instant.minusMillis( 1 ) ;

Half-Open

But I suggest you not take this approach. Do not track a span of time by its last moment.

Your attempt to track the last moment of the day is problematic. You are losing that last millisecond of time. Doing so leaves a gap until the first moment of the next day. And when switching to a finer time slicing such as microseconds used by some systems such as databases like Postgres, and the nanoseconds used by other software such as the java.time classes, you have a worse problem.

A better approach is the Half-Open approach commonly used in date-time handling. The beginning of a span-of-time is inclusive while the ending is exclusive.

So an entire day starts at the first moment of the day, typically at 00:00:00 (but not always!), and runs up to, but does not include, the first moment of the next day.

ZoneId z = ZoneId.of( "Asia/Kolkata" ) ;
ZonedDateTime zdt = ZonedDateTime.now( z ) ;
ZonedDateTime start = zdt.toLocalDate().atStartOfDay( z ) ;
ZonedDateTime stop = start.plusDays( 1 ) ;

Tip: For working with such spans of time, add the ThreeTen-Extra library to access the Interval class.


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.

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

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

You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.* classes.

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.

Upvotes: 5

Mdhar9e
Mdhar9e

Reputation: 1376

yes. i just tried with the above mehtod getTime() returns milliseconds to the given date. and substracting a millisecond is giving me a correct output. new Date(milliseconds) giving me the Date format.

Thanks @Elliott Frisch

Upvotes: 0

Related Questions