0
点赞
收藏
分享

微信扫一扫

【LocalDate LocalTime LocalDateTime】当前日期、当前时间、当前日期时间

49路末班车 2022-02-18 阅读 151
java
package org.example.testTime;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneOffset;

public class TestLocalDateTime {
    public static void main(String[] args) {
        // step1.日期
        LocalDate now = LocalDate.now();
        System.out.println("now=" + now); // now=2022-02-18

        // step2.时间
        LocalTime now1 = LocalTime.now();
        System.out.println("now1=" + now1.withNano(0)); // now1=00:45:19

        // step3.日期+时间
        LocalDateTime now2 = LocalDateTime.now();
        System.out.println("now2=" + now2.withNano(0)); // now2=2022-02-18T00:46:04

        // step4.指定年月日时分秒
        LocalDateTime of = LocalDateTime.of(2008, 8, 8, 20, 8, 8, 0);
        System.out.println("of=" + of); // of=2008-08-08T20:08:08

        // step5.指定年
        LocalDateTime of2 = of.withYear(2022);
        System.out.println("of2=" + of2); // of2=2022-08-08T20:08:08

        // step6.加天
        LocalDateTime of3 = of2.plusDays(2);
        System.out.println("of3=" + of3); // of3=2022-08-10T20:08:08

        // step7.指定本周周几
        int dayOfWeek = 7;
        LocalDateTime ldt = LocalDateTime.now();
        LocalDateTime newTime = ldt.minusDays(ldt.getDayOfWeek().getValue())
                .plusDays(dayOfWeek) // 周6
                .withHour(0)
                .withMinute(0)
                .withSecond(0)
                .withNano(0);
        System.out.println("newTime=" + newTime); // newTime=2022-02-20T00:00

        // step8.获取当前时间戳(毫秒)
        long l = LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli();
        System.out.println("l=" + l); // l=1645116890691
    }
}




举报

相关推荐

0 条评论