Java常用类3

Java常用类3

Java.time.API(jdk8中新时间API)

LocalDate,LocalTime,LocalDateTime的使用

实例化:

  1. 当前时间:now()
  2. 指定时间(没有偏移量):of()

        //实例化
        LocalDateTime now = LocalDateTime.now();//1. now()当前
        System.out.println(now);
        LocalDateTime of = LocalDateTime.of(2020, 10, 6, 13, 23, 43);//2. of指定(没有偏移量)
        System.out.println(of);
        System.out.println("=========================");


        //常用方法
        /*getXxx()*/ //获取一些属性
        System.out.println(now.getDayOfMonth());//这个月的第几天
        System.out.println("=========================");

        /*withXxx()*///设置
        LocalDateTime localDateTime = now.withDayOfMonth(22);//改成22天
        System.out.println(now);//不可变性
        System.out.println(localDateTime);
        System.out.println("=========================");

        /*plusXxx(),minusXxx()*/// 加减
        LocalDateTime localDateTime1 = now.plusMonths(3);//加了3个月
        System.out.println(localDateTime1);
        LocalDateTime localDateTime2 = now.minusDays(6);//减了6天
        System.out.println(localDateTime2);
        System.out.println("=========================");

Inetant瞬时(类似Date )

从1970年开始记录,以秒为单位

实例化:

  1. now():本初子午线的时间
  2. atOffset():添加偏移量(时差)
        //实例化
        Instant now = Instant.now();//now()
        System.out.println(now);//本初子午线的时间
        OffsetDateTime offsetDateTime = now.atOffset(ZoneOffset.ofHours(8));//添加偏移量
        System.out.println(offsetDateTime);
        System.out.println("=========================");

        //常用方法
        /*toEpochMilli*/// 获取从1970(UIT)开始的毫秒数
        long l = now.toEpochMilli();
        System.out.println(l);
        System.out.println("=========================");

        /*ofEpochMilli()*/// 给毫秒,出实例
        Instant instant = Instant.ofEpochMilli(1644979783177L);
        System.out.println(instant);
        System.out.println("=========================")

java.time.format.DateTimeFormatter类(类似于SimpleDateFormat格式化与解析)

实例化

  1. 预定义
  2. 本地化
  3. 自定义(推荐)
        //1.预定义(少)
        DateTimeFormatter isoLocalDateTime = DateTimeFormatter.ISO_LOCAL_DATE_TIME;
        //格式化:日期——>字符串:format
        LocalDateTime now = LocalDateTime.now();
        String format = isoLocalDateTime.format(now);
        System.out.println(format);
        //解析:字符串——>日期:parse
        TemporalAccessor parse = isoLocalDateTime.parse(format);
        System.out.println(parse);
        System.out.println("=========================");
        /*======================================================================*/

        //2.本地化:
        // a. ofLocalizedDateTime (FormatStyle.LONG/FormatStyle.MEDIUM/FormatStyle.SHORT:适用于LocalDateTime)
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT);

        String format1 = dateTimeFormatter.format(now);
        System.out.println(format1);
   
        TemporalAccessor parse1 = dateTimeFormatter.parse(format1);
        System.out.println(parse1);

        //b. ofLocalizedDate (FormatStyle.FULL/FormatStyle.LONG/FormatStyle.MEDIUM/FormatStyle.SHORT:适用于LocalDateTime)
        DateTimeFormatter dateTimeFormatter1 = DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL);

        String format2 = dateTimeFormatter1.format(LocalDate.now());
        System.out.println(format2);

        TemporalAccessor parse2 = dateTimeFormatter1.parse(format2);
        System.out.println(parse2);
        System.out.println("=========================");
        /*======================================================================*/

        //3.自定义ofPattern("yyyy-MM-d")
        DateTimeFormatter dateTimeFormatter2 = DateTimeFormatter.ofPattern("yyyy-MM-dd");

        String format3 = dateTimeFormatter2.format(now);
        System.out.println(format3);

        TemporalAccessor parse3 = dateTimeFormatter2.parse(format3);
        System.out.println(parse3);
        System.out.println("=========================");

posted @ 2022-02-15 22:06  T,a,o  阅读(50)  评论(0编辑  收藏  举报