How to add hours and minutes to Java Instant


To add hours or minutes to the Java Instant Class object we can make use of the plus methods from the Instant class.

Example:
import java.time.Instant;
import java.time.temporal.ChronoUnit;

public class JavaInstantClassExample {

    public static void main(String[] args) {

        int hoursToAdd = 4;
        int minutesToAdd = 30;
        Instant currentTimeStampInUTC = Instant.now();
        System.out.println("Current Time: "+currentTimeStampInUTC);

        System.out.println("Adding 4 hours and 30 minutes to current time");
        Instant laterTime = currentTimeStampInUTC.plus(hoursToAdd,ChronoUnit.HOURS)
                                             .plus(minutesToAdd,ChronoUnit.MINUTES);
        System.out.println("Later Time: "+laterTime);
    }
}
Output:
Current Time: 2022-05-13T18:37:55.991272Z
Adding 4 hours and 30 minutes to current time
Later Time: 2022-05-13T23:07:55.991272Z
Java Instant class add hours and minutes
Advertisements
Try Out Code2care Dev Tools:

Advertisements

Advertisements
Code2care is an initiative to publish and share varied knowledge in programming and technical areas gathered during day-to-day learnings and development activities.

Students and software developers can leverage this portal to find solutions to their various queries without re-inventing the wheel by referring to our easy to understand posts. Technical posts might include learnings, tutorials, trouble-shooting steps, video tutorials, code snippets, how-to, blogs, articles, etc.

🎉 We are celebrating the 10th years of Code2care! Thank you for all your support!

We strongly support Gender Equality & Diversity.