Java Data Type How to - Add hour and minute to instant








Question

We would like to know how to add hour and minute to instant.

Answer

import java.time.Instant;
import java.time.temporal.ChronoUnit;
//from   ww w .  java2 s. com
public class Main {

  public static void main(String[] args) {
    Instant t1 = Instant.now();
    long hours = 2;
    long minutes = 30;
    Instant t2 = t1.plus(hours, ChronoUnit.HOURS).plus(minutes,
        ChronoUnit.MINUTES);

    System.out.println(String.format("now %s and later %s", t1, t2));
  }
}

The code above generates the following result.