集册 Java实例教程 从ZoneDateTime获取本地时间的多种方法

从ZoneDateTime获取本地时间的多种方法

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

479
提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
从ZoneDateTime获取本地时间的多种方法

import java.time.LocalTime;
/*
时 代 J a v a 公 众 号 提供
*/

import java.time.ZonedDateTime;

import java.time.temporal.TemporalQueries;


public class Main {

  public static void main(String[] args) {

    ZonedDateTime zdt = ZonedDateTime.now();


    // Use the toLocalTime() method of the ZonedDateTime class (preferred)

    LocalTime lt1 = zdt.toLocalTime();


    // Use the from() methdo of the LocalTime class

    LocalTime lt2 = LocalTime.from(zdt);


    // Use the localTime() query

    LocalTime lt3 = zdt.query(TemporalQueries.localTime());


    // Use the LocalTime::from method as a query

    LocalTime lt4 = zdt.query(LocalTime::from);/* from nowjava*/


    // Get all time components and construct a LocalTime

    int hours = zdt.getHour();

    int minutes = zdt.getMinute();

    int seconds = zdt.getSecond();

    int nanos = zdt.getNano();

    LocalTime lt5 = LocalTime.of(hours, minutes, seconds, nanos);


    // Print all LocalTimes

    System.out.println("lt1: " + lt1);

    
展开阅读全文