JavaSE常用类日期时间02 2021.06.22-23

努力努力再努力

JDK8 新日期时间API

JDK8之前的日期时间问题:

  • 可变性:
  • 偏移性:Date类中的年份是从1900年开始,月份是从0开始
  • 格式化:格式化对Calendar没用
  • 线程不安全,不能处理闰秒

LocalDate、LocalTime、LocalDateTime

说明:

  • LocalDateTime使用频率较高
  • 类似于Calendar
  • 实例化用 now()或者of()

在这里插入图片描述

        //now():获取当前的日期、时间、日期+时间
        LocalDate localDate = LocalDate.now();
        LocalTime localTime = LocalTime.now();
        LocalDateTime localDateTime = LocalDateTime.now();

        System.out.println(localDate);
        System.out.println(localTime);
        System.out.println(localDateTime);

        //of():设置指定的年、月、日、时、分、秒,没有偏移量
        LocalDateTime localDateTime1 = LocalDateTime.of(2019, 2, 24, 23, 5, 20);
        System.out.println(localDateTime1);//2019-02-24T23:05:20

        //getXxx()
        System.out.println(localDateTime.getDayOfMonth());//23
        System.out.println(localDateTime.getDayOfWeek());//WEDNESDAY
        System.out.println(localDateTime.getMonth());//JUNE
        System.out.println(localDateTime.getMonthValue());//6
        System.out.println(localDateTime.getYear());//2021
        System.out.println(localDateTime.getMinute());//50

        //withXxx():设置相关的属性;体现不可变性
        LocalDateTime localDateTime2 = localDateTime.withMonth(3);
        System.out.println(localDateTime2);//2021-03-23T14:07:32.632
        System.out.println(localDateTime);//2021-06-23T14:07:32.632

        //plus()、minus()
        LocalDateTime localDateTime3 = localDateTime.plusDays(5);
        System.out.println(localDateTime3);//2021-06-28T14:07:32.632
        System.out.println(localDateTime);//2021-06-23T14:07:32.632

        LocalDateTime localDateTime4 = localDateTime.minusDays(5);
        System.out.println(localDateTime4);//2021-06-18T14:07:32.632

Instant 瞬时

类似于 java.util.Date类
在这里插入图片描述

        //now():获取本初子午线对应的标准时间
        Instant instant = Instant.now();
        System.out.println(instant);//2021-06-23T16:31:48.366Z

        //添加时间的偏移量
        OffsetDateTime offsetDateTime = instant.atOffset(ZoneOffset.ofHours(8));
        System.out.println(offsetDateTime);//2021-06-24T00:31:48.366+08:00

        //toEpochMilli():获取自1970年1月1日0时0分0秒(UTC)开始的毫秒数 ---> 类似Date类的getTime()
        long milli = instant.toEpochMilli();
        System.out.println(milli);//1624465898268

        //ofEpochMilli():通过给定的毫秒数,获取Instant实例 ---> 类似Date(long millis)
        Instant instant1 = Instant.ofEpochMilli(1550998346618L);
        System.out.println(instant1);//2019-02-24T08:52:26.618Z

DateTimeFormatter

格式化或解析日期、时间
类似于SimpleDateFormat

//    方式一:预定义的标准格式。如 ISO_LOCAL_DATE_TIME; ISO_LOCAL_DATE; ISO_LOCAL_TIME;
        DateTimeFormatter formatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME;
        //格式化:日期-->字符串
        LocalDateTime localDateTime =LocalDateTime.now();
        String str1 = formatter.format(localDateTime);//LocalDate、LocalTime、LocalDateTime实现了TemporalAccessor接口
        System.out.println(localDateTime);//2021-06-24T00:56:57.029
        System.out.println(str1);//2021-06-24T00:56:57.029
        //解析:字符串-->日期
        TemporalAccessor parse = formatter.parse("2021-06-24T00:56:57.029");//因为返回的不知道是哪一个(LocalDate、LocalTime、LocalDateTime),所以返回值类型以接口形式呈现,
        System.out.println(parse);//{},ISO resolved to 2021-06-24T00:56:57.029

//    方式二:
        //本地化相关的格式。如:ofLocalizedDateTime()
        //FormatStyle.LONG/FormatStyle.MEDIUM/FormatStyle.SHORT:适用于LocalDateTime
        DateTimeFormatter formatter1 = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.LONG);
        String str2 = formatter1.format(localDateTime);
        System.out.println(str2);//LONG:2021年6月24日 上午01时15分38秒; MEDIUM:2021-6-24 1:15:59; SHORT:21-6-24 上午1:16

        TemporalAccessor parse1 = formatter1.parse("2021年6月24日 上午01时15分38秒");
        System.out.println(parse1);//{},ISO resolved to 2021-06-24T01:15:38

        //本地化相关的格式。如:ofLocalizedDate()
        //FormatStyle.FULL/FormatStyle.LONG/FormatStyle.MEDIUM/FormatStyle.SHORT:适用于LocalDate
        DateTimeFormatter formatter2 = DateTimeFormatter.ofLocalizedDate(FormatStyle.LONG);
        String str3 = formatter2.format(LocalDate.now());
        System.out.println(str3);//FUll:2021年6月24日 星期四; LONG:2021年6月24日; MEDIUM:2021-6-24; SHORT:21-6-24;

        TemporalAccessor parse2 = formatter2.parse("2021年6月24日");
        System.out.println(parse2);//{},ISO resolved to 2021-06-24

//    重点:方式三:自定义的格式。如:ofPattern
        //格式化
        DateTimeFormatter formatter3 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String str4 = formatter3.format(LocalDateTime.now());
        System.out.println(str4);//2021-06-24 01:30:51
        //解析
        TemporalAccessor parse3 = formatter3.parse("2021-06-24 01:30:31");
        System.out.println(parse3);//{},ISO resolved to 2021-06-24T01:30:31

其他API

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

与传统日期处理的转换

在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值