Java LocalDateTime add hours

Description

Java LocalDateTime add hours

import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;

public class Main {
  public static void main(String[] args) {
    LocalDateTime now = LocalDateTime.now();

    // Get date and time 4 hours later
    LocalDateTime ldt4 = now.plus(4, ChronoUnit.HOURS);

    // Use the plusHours() method to get the same result
    LocalDateTime ldt5 = now.plusHours(4);

    System.out.println("Current Datetime: " + now);
    System.out.println("4 hours after: " + ldt4);
    System.out.println("4 hours after: " + ldt5);
  }//w w w  .j av a2  s  .co m
}



PreviousNext

Related