Close

Java Date Time - Instant.plus() Examples

Java Date Time Java Java API 


Class:

java.time.Instant

java.lang.Objectjava.lang.Objectjava.time.Instantjava.time.Instantjava.time.temporal.TemporalTemporaljava.time.temporal.TemporalAdjusterTemporalAdjusterjava.lang.ComparableComparablejava.io.SerializableSerializableLogicBig

Methods:

public Instant plus(TemporalAmount amountToAdd)

Returns a copy of this instant with the specified temporal amount added.



public Instant plus(long amountToAdd,
                    TemporalUnit unit)

Returns a copy of this instant with the specified long amount added for the specified unit.


Examples


package com.logicbig.example.instant;

import java.time.Duration;
import java.time.Instant;
import java.time.Period;
public class PlusExample {


public static void main(String... args) {
Instant i = Instant.now();
System.out.println(i);

Instant i2 = i.plus(Duration.ofHours(20));
System.out.println(i2);

Instant i3 = i.plus(Period.ofDays(3));
System.out.println(i3);
}
}

Output

2017-05-01T20:57:20.267Z
2017-05-02T16:57:20.267Z
2017-05-04T20:57:20.267Z




package com.logicbig.example.instant;

import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.time.temporal.UnsupportedTemporalTypeException;

public class PlusExample2 {

public static void main(String... args) {
Instant i = Instant.now();
System.out.println(i);

for (ChronoUnit chronoUnit : ChronoUnit.values()) {
try {
Instant i2 = i.plus(20, chronoUnit);
System.out.printf("%8s > %s%n", chronoUnit, i2);
} catch (UnsupportedTemporalTypeException e) {
System.out.printf("-- %s not supported%n", chronoUnit);
}
}
}
}

Output

2017-05-01T20:57:22.339Z
Nanos > 2017-05-01T20:57:22.339000020Z
Micros > 2017-05-01T20:57:22.339020Z
Millis > 2017-05-01T20:57:22.359Z
Seconds > 2017-05-01T20:57:42.339Z
Minutes > 2017-05-01T21:17:22.339Z
Hours > 2017-05-02T16:57:22.339Z
HalfDays > 2017-05-11T20:57:22.339Z
Days > 2017-05-21T20:57:22.339Z
-- Weeks not supported
-- Months not supported
-- Years not supported
-- Decades not supported
-- Centuries not supported
-- Millennia not supported
-- Eras not supported
-- Forever not supported




See Also