集册 Java实例教程 获取当前日期、时间和日期时间,并构造日期

获取当前日期、时间和日期时间,并构造日期

欢马劈雪     最近更新时间:2020-01-02 10:19:05

494
提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
获取当前日期、时间和日期时间,并构造日期

import java.time.LocalDate;

import java.time.LocalTime;// from 时   代    Java - nowjava.com

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);/*from n o w    j a v a  . c o m*/


    // Construct a birth date and time from date-time components

    LocalDate myBirthDate = LocalDate.of(2000,
展开阅读全文