JDK1.8的LocalDateTime

package com.company;

import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.Optional;

public class Test {
    public static void main(String[] args) {
        //LocalDateTime使用
        LocalDateTime now = LocalDateTime.now();
        System.out.println(now);//2019-08-11T10:50:47.761302
        System.out.println(now.getYear()+":"+now.getMonthValue()+":"+now.getDayOfMonth());//2019:8:11
        LocalDateTime localDateTime = now.plusDays(10);
        System.out.println(localDateTime);//2019-08-21T10:50:47.761302
        LocalDateTime localDateTime1 = localDateTime.minusYears(9);
        System.out.println(localDateTime1);//2010-08-21T10:50:47.761302
        //手动创建一个LocalDateTime
        LocalDateTime date2 = LocalDateTime.of(2017, 12, 17, 9, 31, 31, 31);
        System.out.println(date2);



        // Duration:计算两个时间之间的间隔
        // Period:计算两个日期之间的间隔

        Instant ins1 = Instant.now();

        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        Instant ins2 = Instant.now();
        Duration dura = Duration.between(ins1, ins2);
        /**
         * PT1.00023S
         * 1000
         */
        System.out.println(dura);
        System.out.println(dura.toMillis());

        System.out.println("======================");
        LocalTime localTime = LocalTime.now();
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        LocalTime localTime2 = LocalTime.now();
        Duration du2 = Duration.between(localTime, localTime2);
        /**
         * PT1.002969S
         * 1002
         */
        System.out.println(du2);
        System.out.println(du2.toMillis());


        LocalDate localDate =LocalDate.now();

        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        LocalDate localDate2 = LocalDate.of(2016,12,12);
        Period pe = Period.between(localDate, localDate2);
        System.out.println(pe);

        LocalDateTime now2 = LocalDateTime.now(ZoneId.of("Asia/Shanghai"));
        System.out.println(now2);

        LocalDateTime now3 = LocalDateTime.now();
        ZonedDateTime zdt = now3.atZone(ZoneId.of("Asia/Shanghai"));
        System.out.println(zdt);

        //时间戳
        //转化为时间戳  毫秒值
        long time1 = LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli();
        long time2 = System.currentTimeMillis();
        System.out.println(time1+"+++"+time2);
        //时间戳转化为localdatetime
        DateTimeFormatter df= DateTimeFormatter.ofPattern("YYYY-MM-dd HH:mm:ss.SSS");

        System.out.println(df.format(LocalDateTime.ofInstant(Instant.ofEpochMilli(time1),ZoneId.of("Asia/Shanghai"))));
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值