Obtaining Current Date, Time, and Datetime, and Constructing a Date - Java Date Time

Java examples for Date Time:Local Date Time

Description

Obtaining Current Date, Time, and Datetime, and Constructing a Date

Demo Code

import java.time.LocalDate;
import java.time.LocalTime;
import java.time.LocalDateTime;
import java.time.ZonedDateTime;
import static java.time.Month.JANUARY;

public class Main {
  public static void main(String[] args) {
    // Get current date, time, and datetime
    LocalDate dateOnly = LocalDate.now();
    LocalTime timeOnly = LocalTime.now();
    LocalDateTime dateTime = LocalDateTime.now();
    ZonedDateTime dateTimeWithZone = ZonedDateTime.now();

    System.out.println("Current Date: " + dateOnly);
    System.out.println("Current Time: " + timeOnly);
    System.out.println("Current Date and Time: " + dateTime);
    System.out.println("Current Date, Time, and Zone: " + dateTimeWithZone);

    // Construct a birth date and time from date-time components
    LocalDate myBirthDate = LocalDate.of(2000, JANUARY, 12);
    LocalTime myBirthTime = LocalTime.of(7, 30);
    System.out.println("My Birth Date: " + myBirthDate);
    System.out.println("My Birth Time:: " + myBirthTime);
  }//  ww  w  .ja va 2s.  co m
}

Result


Related Tutorials