JDK8的日期与时间类

JDK8的日期与时间类

  • java.time包,包含了更多的日期和时间操作类,代码如下:
package com.sgl;

import java.time.*;

public class Test {
    public static void main(String[] args){
        long startTime = System.currentTimeMillis(); //循环开始时的当前时间

        //1.Clock的使用
        Clock clock = Clock.systemUTC();
        System.out.println("获取UTC时区转换的当前时间:"+clock.instant());
        System.out.println("获取UTC时区转换的毫秒数:"+clock.millis());
        System.out.println("======================================");
        //2.Duration的使用
        Duration duration = Duration.ofDays(1);
        System.out.println("一天等于"+duration.toHours()+"小时");
        System.out.println("一天等于"+duration.toMinutes()+"分钟");
        System.out.println("一天等于"+duration.toMillis()+"秒");
        System.out.println("======================================");
        //3.Instant的使用
        Instant instant = Instant.now();
        System.out.println("获取UTC当前的时间为:"+instant);
        System.out.println("当前一小时后的时间为:"+instant.plusSeconds(3600));
        System.out.println("当前一小时前的时间为:"+instant.minusSeconds(3600));
        System.out.println("======================================");
        //4.LocalDate的使用
        LocalDate localDate = LocalDate.now();
        System.out.println("从默认时区的系统时钟获取当前日期:"+localDate);
        System.out.println("======================================");
        //5.LocalTime的使用
        LocalTime localTime = LocalTime.now();
        System.out.println("从默认时区的系统时钟获取当前日期:"+localTime);
        System.out.println("======================================");
        //6.LocalDateTime的使用
        LocalDateTime localDateTime = LocalDateTime.now();
        System.out.println("从默认时区的系统获取日期,时间:"+localDateTime);
        LocalDateTime localDateTime1 = localDateTime.plusDays(1).plusHours(3).plusMinutes(30);
        System.out.println("当前的日期,时间加上1天3小时30分之后:"+localDateTime1);
        System.out.println("======================================");
        //7.Year,YearMonth,MonthDay的使用
        Year year = Year.now();
        System.out.println("当前年份为:"+year);
        YearMonth yearMonth = YearMonth.now();
        System.out.println("当前月份为:"+yearMonth);
        MonthDay monthDay = MonthDay.now();
        System.out.println("当前月日为:"+monthDay);
        System.out.println("======================================");
        //8.获取系统默认时区
        ZoneId zoneId = ZoneId.systemDefault();
        System.out.println("当前系统默认时区为:"+zoneId);
        System.out.println("======================================");


        long endTime = System.currentTimeMillis();  //循环结束时的当前时间
        System.out.println("程序运行时间为:"+(endTime - startTime)+"毫秒");  //程序运行时间为:28毫秒   值不确定
    }

}

运行结果:

获取UTC时区转换的当前时间:2021-08-02T02:47:04.639Z
获取UTC时区转换的毫秒数:1627872424768
======================================
一天等于24小时
一天等于1440分钟
一天等于86400000======================================
获取UTC当前的时间为:2021-08-02T02:47:04.768Z
当前一小时后的时间为:2021-08-02T03:47:04.768Z
当前一小时前的时间为:2021-08-02T01:47:04.768Z
======================================
从默认时区的系统时钟获取当前日期:2021-08-02
======================================
从默认时区的系统时钟获取当前日期:10:47:04.798
======================================
从默认时区的系统获取日期,时间:2021-08-02T10:47:04.798
当前的日期,时间加上13小时30分之后:2021-08-03T14:17:04.798
======================================
当前年份为:2021
当前月份为:2021-08
当前月日为:--08-02
======================================
当前系统默认时区为:Asia/Shanghai
======================================
程序运行时间为:159毫秒
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

等慢慢

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值