Java8的新特性--新时间日期API


Java8提供了一套全新的日期时间API,都位于java.time包下,这个包涵盖了所有处理日期date、时间time、日期/时间dateTime、时区zone、时间戳instant、间隔duration、时钟clock等操作。

一、简单介绍

  • LocalDateTime:表示没有时区的日期时间,LocalDateTime是不可变并且线程安全的
  • LocalDate:表示没有时区的日期,LocalDate是不可变并且线程安全的
  • LocalTime:表示没有时区的时间,LocalTime是不可变并且线程安全的
  • Instant:用来表示时间线上的一个点(瞬时)
  • ZoneId:时区ID,用来确定Instant和LocalDateTime互相转换的规则
  • Clock:用于访问当前时刻、日期、时间,用到时区
  • Duration:用秒和纳秒表示时间的数量(长短),用于计算两个日期的“时间”间隔
  • Period:用于两个“日期”间隔

二、常见用法示例

2.1 LocalDateTime、 LocalDate、 LocalTime

2.1.1 获取当前时间
@Test
public void teat01(){

        //LocalDateTime(日期时间) LocalDate(日期) LocalTime(时间)
        // 类的实例变量是不可变得对象,所以是线程安全的,它们分别表示使
        // 用ISO-8601日历系统的时间(ISO-8601日历系统是国际标准化组织制定的现代公民的日期和时间的表示法)
        // 都位于java.time包下

        //获取当前时间
        LocalDateTime now = LocalDateTime.now();
        LocalDate now1 = LocalDate.now();
        LocalTime now2 = LocalTime.now();
        System.out.println("【当前日期时间】"+now);
        System.out.println("【当前日期】"+now1);
        System.out.println("【当前时间】"+now2);

        //LocalDate  指定一个日期
        LocalDate localDate = LocalDate.now();
        System.out.println("【当前日期】"+localDate);
        LocalDate year = localDate.withDayOfYear(100);
        System.out.println("【当前日期为基础,指定本年当中的第几天,1-31号】"+year);
        LocalDate month = localDate.withDayOfMonth(2);
        System.out.println("【当前日期为基础,指定本月当中的第几月,1-31号】"+month);
        System.out.println("【当前日期为基础,直接指定月份】"+LocalDate.now().withMonth(5));

        //LocalDateTime 获取当前日期
        System.out.println("【当前时间】"+LocalDateTime.now());
        System.out.println("【当前时间是本年的第几天】"+LocalDateTime.now().getDayOfYear());
        System.out.println("【当前时间是本月的第几个天】"+LocalDateTime.now().getDayOfMonth());
        System.out.println("【当前时间是周几】"+LocalDateTime.now().getDayOfWeek());
        //获取年月日    当前时间】2021年MARCH月20日9时43分43秒
        System.out.println("【当前时间】"+LocalDateTime.now().getYear()+"年"+LocalDateTime.now().getMonth()+"月"+LocalDateTime.now().getDayOfMonth() +"日"
                +LocalDateTime.now().getHour()+"时"+LocalDateTime.now().getMinute()+"分"+LocalDateTime.now().getSecond()+"秒");

    }

运行结果

在这里插入图片描述

2.1.2 时间日期的比较和运算判断
   @Test
    public void teat02(){

        System.out.println("【当前时间加一年加一天】"+LocalDateTime.now().plusYears(1).plusDays(1));
        System.out.println("【当前时间减俩月】"+LocalDateTime.now().minusMonths(2));

        LocalDate localDate1 = LocalDate.of(2021, 03, 20);
        LocalDate localDate2 = LocalDate.of(2020, 03, 20);
        System.out.println(localDate1.isBefore(localDate2));

        //判断是否闰年
        System.out.println(LocalDate.now()+"【当前时间是否闰年】"+LocalDate.now().isLeapYear());

    }

运行结果

在这里插入图片描述

2.1.3 时间日期的转换
    @Test
    public void teat05(){

        LocalDateTime now = LocalDateTime.now();
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH:mm:ss");
        String format = dateTimeFormatter.format(now);
        System.out.println(now);
        System.out.println(format);

    }

运行结果

在这里插入图片描述

2.2 Instant 时间戳

时间戳对应的是java.time.instant,专门用于时间戳的运算,时间戳是指是指:从Unix元年(传统的设定为UTC时区1970年1月1日午夜时分)开始到某个时间之间的毫秒值。另外,时间戳是给计算机读的不是给人读的。

时间戳实际上就是Java8以前的Date,两者可以互相转换。

Date.from(Instant.now())把Instant转成java,util,date

new Date().toInstant()把Date转成Instant

    //时间戳
    @Test
    public void teat03(){
        //默认获取的是以UTC时区(世界协调时间,也叫格林威治时间)为基础的时间戳
        //中国在东八区,所以这个输出的时间是差了八个时区的
        Instant instant = Instant.now();
        System.out.println("【当前时间时间戳为】"+instant);

        //做一个偏移量的运算   偏移八个时区即为中国时间
        OffsetDateTime offsetDateTime = instant.atOffset(ZoneOffset.ofHours(8));
        System.out.println("【当前时间时间戳偏移八个时区】"+offsetDateTime);

        System.out.println("当前时间的毫秒值"+Instant.now().toEpochMilli());
        System.out.println("系统当前的毫秒值"+System.currentTimeMillis());
        
        //相互转换
        //Date.from(Instant.now())把Instant转成java,util,date
        Date date = Date.from(Instant.now());
        System.out.println("【date】 "+date);
        //new Date().toInstant()把Date转成Instant
        Instant instant1 = date.toInstant();
        System.out.println("【instant】"+instant1);
    }

运行结果

在这里插入图片描述

2.3 ZoneId

Java8中加入了对时区的支持,带时区的时间分别为ZoneDate、ZoneTime、ZoneDateTime.其中每个时区都对应着地区ID,地区ID都为“{区域}/{城市}”的格式,比如Asia/Shanghai.

java.time.ZoneId的类包含了所有的时区信息,下面我们来看下支持的时区都是有哪些。

    //ZoneId
    @Test
    public void test03(){
        //可以查看所有Java8中支持的时区
        Set<String> availableZoneIds = ZoneId.getAvailableZoneIds();
        System.out.println("支持的时区个数:"+availableZoneIds.size());
        //指定一个时区去构建一个日期
        LocalDateTime ldt = LocalDateTime.now(ZoneId.of("Asia/Shanghai"));
        System.out.println(ldt);
        //构建出一个带时区的时间日期
        ZonedDateTime zdt = ldt.atZone(ZoneId.of("Asia/Shanghai"));
        System.out.println(zdt);
    }

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

2.5 Duration、Period 计算时间间隔

Period计算两个日期之间的间隔

Duration计算两个时间或时间戳之间的间隔

    @Test
    public void test04(){
        Instant instant1 = Instant.now();
        LocalDateTime localDateTime1 = LocalDateTime.now();

        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        Instant instant2 = Instant.now();
        LocalDateTime localDateTime2 = LocalDateTime.now();
        //时间戳的间隔
        System.out.println(Duration.between(instant1,instant2));
        System.out.println(Duration.between(instant2,instant1));

        //日期时间的间隔
        System.out.println(Duration.between(localDateTime1,localDateTime2));
        //获取毫秒值
        System.out.println("时间戳的间隔计算 "+Duration.between(instant1,instant2).toMillis());
        System.out.println("日期时间的间隔计算 "+Duration.between(localDateTime1,localDateTime2).toMillis());

        //Period
        LocalDate ld1 = LocalDate.of(2020, 3, 20);
        LocalDate ld2 = LocalDate.now();

        Period period = Period.between(ld1, ld2);
        System.out.println("Period计算日期间隔 "+period);
        System.out.println(period.getYears()+"年"+period.getMonths()+"月"+period.getDays()+"日");

    }

运行结果

在这里插入图片描述

2.6 TemporalAdjuster 时间矫正器

TemporalAdjuster时间矫正器可以用来执行一些特殊的操作,比如要获取下一个工作日,下一个周五等操作。

TemporalAdjusters类通过静态方法提供了大量常用TemporalAdjuster的实现。

   @Test
    public void teat08(){

        LocalDateTime localDateTime = LocalDateTime.now();
        LocalDateTime with = localDateTime.with(TemporalAdjusters.next(DayOfWeek.FRIDAY));
        //下一个周五
        System.out.println(with);//打印输出 2021-03-26T11:31:53.682
    }

总结,日期时间完结。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值