Java ChronoUnit enum

Introduction

ChronoUnit contains the following constants to represent units of time:

  • CENTURIES
  • DAYS
  • DECADES
  • ERAS
  • FOREVER
  • HALF_DAYS
  • HOURS
  • MICROS
  • MILLENNIA
  • MILLIS
  • MINUTES
  • MONTHS
  • NANOS
  • SECONDS
  • WEEKS
  • YEARS
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;

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

    // Get the date time 4 days ago
    LocalDateTime ldt2 = now.minus(4, ChronoUnit.DAYS);

    System.out.println("Current Datetime: " + now);
    System.out.println("4 days ago: " + ldt2);
  }/* w  w  w  . j  av  a2  s .c o  m*/
}



PreviousNext

Related