now() Method

Description

now() method returns the current time for various class, for example LocalDate, LocalTime, LocalDateTime, ZonedDateTime.

Example

The following code shows how to use now() method to return current date and time.


import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZonedDateTime;
//ww  w  .j  a v a  2 s  .  c  om
public class Main {
  public static void main(String[] args) {
    LocalDate localDate = LocalDate.now(); 
    System.out.println(localDate);
    
    LocalTime  localTime  = LocalTime.now();
    System.out.println(localTime);
    
    LocalDateTime  dateTime  = LocalDateTime.now();
    System.out.println(dateTime);
    
    ZonedDateTime dateTimeWithZone  = ZonedDateTime.now();
    System.out.println(dateTimeWithZone);
  }
}

The code above generates the following result.





















Home »
  Java Date Time »
    Tutorial »




Java Date Time Tutorial