java8之localDateTime, ZoneId, Instant使用

import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoField;
import java.time.temporal.TemporalAdjusters;
import java.util.Date;

public class Main {
    public static void main(String[] args) {
        // instant使用
        System.out.println("instant使用————————————");
        System.out.println(Instant.now().toEpochMilli());
        System.out.println(new Date().getTime());
        Instant instant = Instant.now();
        System.out.println(instant);
        System.out.println("**********************");
       
        //LocalDateTime - > long
        System.out.println("LocalDateTime -> long使用");
        System.out.println(LocalDateTime.now().atZone(Clock.systemDefaultZone().getZone()).toInstant().toEpochMilli());
        System.out.println(LocalDateTime.now().toInstant(ZoneOffset.ofHours(8)).toEpochMilli());
        System.out.println("**********************");
       

        //long -> LocalDateTime
        System.out.println("long -> LocalDateTime使用");
        DateTimeFormatter dateTimeFormatter= DateTimeFormatter.ofPattern("YYYY-MM-dd HH:mm:ss");
        String st = dateTimeFormatter.format(LocalDateTime.ofInstant(Instant.ofEpochMilli(new Date().getTime()), ZoneId.of("Asia/Shanghai")));
        System.out.println(st);
        System.out.println("***********************");
       

        //ZonedDateTime
        System.out.println(Instant.now().atZone(ZoneId.systemDefault()).toLocalDateTime());

        //TemporalField 和 TemporalAdjusters
        System.out.println("TemporalField 和 TemporalAdjusters使用");
        LocalDateTime localDateTime1 = LocalDateTime.now();
        System.out.println(localDateTime1.plusDays(1));
        System.out.println(localDateTime1.with(ChronoField.YEAR,2013));
        System.out.println(localDateTime1.with(ChronoField.DAY_OF_YEAR,20));
        System.out.println(localDateTime1.with(ChronoField.MONTH_OF_YEAR,12));
        System.out.println(localDateTime1.with(TemporalAdjusters.firstDayOfMonth()));
        System.out.println(localDateTime1.get(ChronoField.YEAR));
        System.out.println(localDateTime1.get(ChronoField.MONTH_OF_YEAR));
        System.out.println(localDateTime1.get(ChronoField.DAY_OF_YEAR));
        System.out.println(localDateTime1.get(ChronoField.HOUR_OF_AMPM));
        System.out.println(localDateTime1.minusDays(1));
        System.out.println("*************************");
        

        //Date -> String
        System.out.println("Date -> String使用");
        DateTimeFormatter df= DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss");
        String string = df.format(LocalDateTime.ofInstant(Instant.ofEpochMilli(new Date().getTime()),ZoneId.of("Asia/Shanghai")));
        System.out.println(string);
        
    }
}

instant使用————————————
1517311132643
1517311132643
2018-01-30T11:18:52.643Z
**********************

LocalDateTime -> long使用
1517311132734
1517311132735
**********************

long -> LocalDateTime使用
2018-01-30 19:18:52
***********************

2018-01-30T19:18:52.743
TemporalField 和 TemporalAdjusters使用
2018-01-31T19:18:52.744
2013-01-30T19:18:52.744
2018-01-20T19:18:52.744
2018-12-30T19:18:52.744
2018-01-01T19:18:52.744
2018
1
30
7
2018-01-29T19:18:52.744
*************************

Date -> String使用
2018-01-30 07:18:52

    原文作者:will的猜想
    原文地址: https://blog.csdn.net/u012129558/article/details/79209715
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞