java8时间API

java8时间API

  • 1 例子

1 例子

package com.javaTechnology;

import java.time.*;
import java.time.temporal.ChronoUnit;
import java.util.Set;

/**
 * @author LanceQ
 * @date 2022年05月29日 10:54
 * 任何方法都不会更改所操作的类
 */
public class TimeTest {
    public static void main(String[] args) {
        test(10);
        t1();
        t2();
        t3();
        t4();
    }
    /**
     * 时区
     */
    private static void t4() {
        ZonedDateTime now = ZonedDateTime.now();
        //输出当前时间包括时区:2022-05-29T11:55:12.764+08:00[Asia/Shanghai]
        System.out.println("输出当前时间包括时区:"+now);
        Set<String> availableZoneIds = ZoneId.getAvailableZoneIds();
        System.out.println("输出所有时区:"+availableZoneIds);

        ZonedDateTime of = ZonedDateTime.of(1969, 7, 16, 9, 32, 0, 0, ZoneId.of("Asia/Shanghai"));
        //输出时间包括时区:1969-07-16T09:32+08:00[Asia/Shanghai]
        System.out.println("输出时间包括时区:"+of);
        Instant instant = of.toInstant();
        //转换为默认时区:1969-07-16T01:32:00Z
        System.out.println("转换为默认时区:"+instant);

    }
    /**
     * 天数
     */
    private static void t3() {
        //获取当前天数
        LocalDate today = LocalDate.now();
        //2022-05-29
        System.out.println(today);
        LocalDate alonzosBirthday = LocalDate.of(1983, Month.JUNE, 14);
        //1983-06-14
        System.out.println(alonzosBirthday);
        //到现在经过了多少天
        long until = alonzosBirthday.until(today, ChronoUnit.DAYS);
        //到现在经过了多少天:14229
        System.out.println("到现在经过了多少天:"+until);
        //(从1到7)
        DayOfWeek dayOfWeek = today.getDayOfWeek();
        //今天星期几:7
        System.out.println("今天星期几:"+dayOfWeek.getValue());
        //星期六的3天后是星期几:2
        System.out.println("星期六的3天后是星期几:"+DayOfWeek.SATURDAY.plus(3).getValue());

        //得到255天后的时间
        LocalDate programmersDay = today.plusDays(255);
        //得到255天后的时间:2023-02-08
        System.out.println("得到255天后的时间:"+programmersDay);

        LocalTime now = LocalTime.now();
        //获取当前时间:11:55:12.756
        System.out.println("获取当前时间:"+now);
    }

    private static void t2() {
        //检查一个算法的速度是否是另一个算法的10倍以上
        Duration test = test(90);
        Duration test1 = test(1000);
        boolean overTheTimesFaster = test.multipliedBy(10).minus(test1).isNegative();
        //true
        System.out.println(overTheTimesFaster);

        boolean overTheTimesFaster1 = test.toNanos() * 10 < test1.toNanos();
        //true
        System.out.println(overTheTimesFaster1);
    }

    private static void t1() {
        //增加8小时
        Instant start = Instant.now();
        //增加后才是东8区的时间
        Instant later = start.plus(Duration.ofHours(8));
        System.out.println(start);
        //2022-05-29T03:52:27.878Z
        System.out.println(later);
        //2022-05-29T11:52:27.878Z
    }
    /**
     * 区间毫秒
     */
    private static Duration test(int number) {
        Instant start = Instant.now();
        runAlgorithm(number);
        Instant end = Instant.now();
        Duration timeElapsed = Duration.between(start, end);
        long millis = timeElapsed.toMillis();
        System.out.println("计算过去了多少时间毫秒:"+millis);
        return timeElapsed;
    }
    /**
     * 睡眠时间
     */
    private static void runAlgorithm(int number) {
        try {
            Thread.sleep(number);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

你可能感兴趣的:(基础知识,java,开发语言)