集册 Java实例教程 使用偏移日期、时间和日期时间

使用偏移日期、时间和日期时间

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

546
提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
使用偏移日期、时间和日期时间

import java.time.Instant;

import java.time.LocalDate;

import java.time.LocalTime;

import java.time.OffsetDateTime;/**来 自 时 代 J a v a 公 众 号 - nowjava.com**/

import java.time.OffsetTime;

import java.time.ZoneId;

import java.time.ZoneOffset;


public class Main {

  public static void main(String[] args) {

    // Creete a zone-offset +05:30

    ZoneOffset offset = ZoneOffset.ofHoursMinutes(5, 30);


    // Get the current offset time

    OffsetTime ot1 = OffsetTime.now();

    System.out.println("Current offset time: " + ot1);


    // Create an offset time
    /**
     from
    * NowJava.com - 时  代  Java 
    **/

    OffsetTime ot2 = OffsetTime.of(16, 40, 28, 0, offset);

    System.out.println("An offset time: " + ot2);


    // Get the current offset datetime

    OffsetDateTime odt1 = OffsetDateTime.now();

    System.out.println("Current offset datetime: " + odt1);


    // Create an offset datetime

    OffsetDateTime odt2 = OffsetDateTime.of(2019, 5, 11,

                                            18, 10, 30, 0,

                                            offset);

    System.out.println("An offset datetime: " + odt2);


    // Get the local date and time from the offset datetime

    LocalDate ld1 = odt1.toLocalDate();

    LocalTime lt1 = odt1.toLocalTime();

    System.out.println("Current Local Date: " + ld1);

    System.out.println("Current Local Time: " + lt1);


    // Get the instant from the offset datetime

    Instant i1 = odt1.toInstant();

    System.out.
展开阅读全文