Java Data Type How to - Get today's date/time in Java 8








Question

We would like to know how to get today's date/time in Java 8.

Answer

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
/*  ww  w .  j av a 2  s  .c o m*/
public class Main{
  public static void main(String[] argv){

    LocalDate today = LocalDate.now();
    LocalTime time = LocalTime.now();
    LocalDateTime timeWithDate = LocalDateTime.now();

    System.out.println("today = " + today);
    System.out.println("local date with time now = " + timeWithDate);
    System.out.println("time = " + time);
  }
}

The code above generates the following result.