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
More Posts related to Java,
- Deep Dive into Java 8 Predicate Interface
- Read and Parse XML file using Java DOM Parser [Java Tutorial]
- Java 8 Predicate Functional Interface isEqual() Method Example
- Convert Multidimensional Array toString In Java
- How to read int value using Scanner Class Java
- Spring Boot AI + LLM + Java Code Example
- Write to a File using Java Stream API
- Implementing Bubble Sort Algorithm using Java Program
- How to Fix XmlBeanDefinitionStoreException in Java SpringBoot ApplicationConfig.xml
- YAML Parser using Java Jackson Library Example
- [Fix] java: integer number too large compilation error
- Convert JSON String to Java GSON Object Example
- Read a file using Java 8 Stream
- Java Spring Boot 3 Web Hello World with Gradle in IntelliJ
- Ways Compare Dates in Java Programming with Examples
- Pretty Print JSON String in Java Console Output
- Java JDBC with Join Queries Example
- How to Check For Updates on Windows 11 (Step-by-Step)
- [Fix] java.net.MalformedURLException: unknown protocol
- How to display date and time in GMT Timezone in Java
- Error: LinkageError occurred while loading main class UnsupportedClassVersionError [Eclipse Java]
- How to convert a String to Java 8 Stream of Char?
- RabbitMQ Queue Listener Java Spring Boot Code Example
- 5+ Fibonacci number Series Java Program Examples [ 0 1 1 2 3 ..]
- Handling NullPointerException with Java Predicate
More Posts:
- Setting up Java JUnit Project with Eclipse + Maven Example - Java
- Fix: ValueError: unsupported pickle protocol: 5 - Python
- How to clear ZSH history of commands executed on Mac Terminal - zsh
- Steps of Installing MongoDB Community Edition on Ubuntu - Article
- A Deep Dive into HTML Header Tags (H1-H6) - CSS
- Setting up Zsh Syntax Highlighting - zsh
- When to use of() and ofNullable() methods of Optional in Java? - Java
- Convert JSON to XML using Java Jackson Library - Java