Java Instant minus() method

The minus() method of Java Instant class is used to return a copy of this instant with the specified amount subtracted.

The Instant minus() method consists of 2 type of parameters:

  • Java Instant minus(TemporalAmount amountToSubtract) method
  • Java Instant minus(long amountToSubtract, TemporalUnit unit)

The minus(TemporalAmount amountToSubtract) method returns an Instant, based on this one, with the specified amount subtracted. The amount is typically Duration but may be any other type implementing the TemporalAmount interface.

The minus(long amountToSubtract, TemporalUnit unit) returns an Instant, based on this one, with the amount in terms of the unit subtracted. If it is not possible to subtract the amount, because the unit is not supported or for some other reason, an exception is thrown.

Syntax

Parameters

amountToSubtract - the amount to subtract, not null

amountToSubtract - the amount of the unit to subtract from the result, may be negative.

unit - the unit of the amount to subtract, not null.

Return

An Instant based on this instant with the subtraction made, not null

An Instant based on this instant with the specified amount subtracted, not null.

Exceptions

DateTimeException - if the subtraction cannot be made

ArithmeticException - if numeric overflow occurs

DateTimeException - if the subtraction cannot be made

UnsupportedTemporalTypeException - if the unit is not supported

ArithmeticException - if numeric overflow occurs

Example 1

Output:

The output will be like this.

2017-02-03T10:27:30Z

Example 2

Output:

The output will be like this.

2014-08-25T10:15:30Z

Example 3

Output:

The output will be like this.

2014-12-03T10:15:30Z

Example 4

Output:

The output will be like this.

2014-12-03T10:15:33Z

Example 5

Output:

The output will be like this.

2017-05-01T20:57:43.045Z

   Nanos > 2017-05-01T20:57:43.044999990Z

  Micros > 2017-05-01T20:57:43.044990Z

  Millis > 2017-05-01T20:57:43.035Z

 Seconds > 2017-05-01T20:57:33.045Z

 Minutes > 2017-05-01T20:47:43.045Z

   Hours > 2017-05-01T10:57:43.045Z

HalfDays > 2017-04-26T20:57:43.045Z

    Days > 2017-04-21T20:57:43.045Z

      -- Weeks not supported

      -- Months not supported

      -- Years not supported

      -- Decades not supported

      -- Centuries not supported

      -- Millennia not supported

      -- Eras not supported

      -- Forever not supported

Examples Java Instant minus(long amountToSubtract, TemporalUnit unit) Method

Example 6

Output:

The output will be like this.

2014-08-25T10:15:30Z

Example 7

Output:

The output will be like this.

2017-02-03T05:37:30Z