新时间日期API

使用传统的时间api会有线程安全问题,故java8有新时间日期api

1.使用LocalDate、LocalTime、LocalDateTime(三个类似)(人读取的时间)

public void test1(){
        LocalDateTime ldt = LocalDateTime.now();//获取当前时间
        System.out.println(ldt);
        //根据指定日期/时间创建对象
        LocalDateTime ld2 = LocalDateTime.of(2022, 06, 9, 16, 10, 10);
        System.out.println(ld2);
        //加年份
        LocalDateTime ldt3 = ldt.plusYears(20);
        System.out.println(ldt3);
        //减月份
        LocalDateTime ldt4 = ldt.minusMonths(2);
        System.out.println(ldt4);
        System.out.println(ldt.getYear());//获取年份
        System.out.println(ldt.getMonthValue());//获取月份
        System.out.println(ldt.getDayOfMonth());//获取日期
        System.out.println(ldt.getHour());//获取小时
        System.out.println(ldt.getMinute());//获取分钟
        System.out.println(ldt.getSecond());//获取秒数
    }

运行结果:
在这里插入图片描述

2.Instant:时间戳。使用Unix元年 1970年1月1日 00:00:00 所经历的毫秒值)(计算机读取时间)

public void test2(){
        Instant ins = Instant.now();//默认使用 UTC 时区
        System.out.println(ins);
        OffsetDateTime odt = ins.atOffset(ZoneOffset.ofHours(8));//偏移8小时
        System.out.println(odt);
        System.out.println(ins.toEpochMilli());//毫秒
        System.out.println(ins.getNano());//纳秒
        Instant ins2 = Instant.ofEpochSecond(5);
        System.out.println(ins2);
    }

运行结果:
在这里插入图片描述

3.Duration : 用于计算两个“时间”间隔,Period : 用于计算两个“日期”间隔

public void test3(){
        Instant ins1 = Instant.now();
        try { Thread.sleep(1000); } catch (InterruptedException e) { }
        Instant ins2 = Instant.now();
        Duration d = Duration.between(ins1, ins2);
        System.out.println(d.toMillis());//毫秒
        System.out.println("所耗费时间为:" + d);
        System.out.println("----------------------------------");
        LocalDate ld1 = LocalDate.now();
        System.out.println(ld1);
        LocalDate ld2 = LocalDate.of(2011, 1, 1);
        Period pe = Period.between(ld2, ld1);
        System.out.println(pe.getYears());//两个日期相差几年
        System.out.println(pe.getMonths());//两个日期相差几个月
        System.out.println(pe.getDays());//两个日期相差几天
    }

运行结果
在这里插入图片描述

4.TemporalAdjuster : 时间校正器

有时我们可能需要获取例如:将日期调整到“下个周日”等操作。
TemporalAdjusters : 该类通过静态方法提供了大量的常用 TemporalAdjuster 的实现。

public void test4(){
        LocalDateTime ldt = LocalDateTime.now();
        System.out.println(ldt);
        LocalDateTime ldt2 = ldt.withDayOfMonth(20);//将月份天数修改为指定的值并返回新的LocalDate 对象
        System.out.println(ldt2);
        LocalDateTime ldt3 = ldt.with(TemporalAdjusters.next(DayOfWeek.FRIDAY));//下一个周五
        System.out.println(ldt3);
        //自定义:下一个工作日
        LocalDateTime ldt5 = ldt.with((l) -> {
            LocalDateTime ldt4 = (LocalDateTime) l;
            DayOfWeek dow = ldt4.getDayOfWeek();//获取当前是周几
            if(dow.equals(DayOfWeek.FRIDAY)){
                return ldt4.plusDays(3);
            }else if(dow.equals(DayOfWeek.SATURDAY)){
                return ldt4.plusDays(2);
            }else{
                return ldt4.plusDays(1);
            }
        });
        System.out.println(ldt5);
    }

5.DateTimeFormatter : 解析和格式化日期或时间

public void test5(){
		//DateTimeFormatter dtfwww = DateTimeFormatter.ISO_LOCAL_DATE;
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH:mm:ss E");
        LocalDateTime ldt = LocalDateTime.now();
        String strDate = ldt.format(dtf);//时间格式转换为字符串
        System.out.println(strDate);
        LocalDateTime newLdt = ldt.parse(strDate, dtf);//字符串转换为时间格式
        System.out.println(newLdt);
    }

运行结果:
在这里插入图片描述

6.ZonedDate、ZonedTime、ZonedDateTime 带时区的时间或日期

Java8 中加入了对时区的支持,带时区的时间为分别为:ZonedDate、ZonedTime、ZonedDateTime
其中每个时区都对应着 ID,地区ID都为 “{区域}/{城市}”的格式
例如 :Asia/Shanghai 等
ZoneId:该类中包含了所有的时区信息
getAvailableZoneIds() : 可以获取所有时区时区信息
of(id) : 用指定的时区信息获取ZoneId 对象

public void test7(){
        LocalDateTime ldt = LocalDateTime.now(ZoneId.of("Asia/Shanghai"));
        System.out.println(ldt);
        ZonedDateTime zdt = ZonedDateTime.now(ZoneId.of("Asia/Shanghai"));
        System.out.println(zdt);
    }

在这里插入图片描述


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值