栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java8时间API

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java8时间API

java8时间API
  • 1 例子

1 例子
package com.javaTechnology;

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


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 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();
        }
    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/936628.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号