zoukankan      html  css  js  c++  java
  • Jdk8新特性 【时间】 api LocalDate,LocalTime,LocalDateTime

    LocalDate,LocalTime,LocalDateTime

    接口api: https://docs.oracle.com/javase/8/docs/api/java/time/LocalDate.html

    java8时间:LocalDate、LocalTime、LocalDateTime【java8新提供的类】
    使用方式,包括创建、获取、格式化、解析、计算、修改

       @org.junit.Test
        public void testJdk8Time(){
            creat();
            get();
            calculate();
            format();
            parse();
        }

    创建

    public void creat(){
            //创建
            LocalDate nowDate = LocalDate.now(); //直接获取
            LocalDate nowDateOf = LocalDate.of(2020, 2, 25);//指定年-月-日
            LocalDate nowDateTo = LocalDateTime.now().toLocalDate();
            System.out.println(nowDate); //2020-02-25
            System.out.println(nowDateOf); //2020-02-25
            System.out.println(nowDateTo); //2020-02-25
    
            LocalTime nowTime = LocalTime.now();
            LocalTime nowTimeOf = LocalTime.of(13, 50, 10);
            LocalTime nowTimeTo = LocalDateTime.now().toLocalTime();
            System.out.println(nowTime);
            System.out.println(nowTimeOf);
            System.out.println(nowTimeTo);
    
            LocalDateTime nowDateTime = LocalDateTime.now();
            LocalDateTime nowDateTimeOf = LocalDateTime.of(2020, 2, 25, 13, 50, 10);
            System.out.println(nowDateTime);
            System.out.println(nowDateTimeOf);
    }

    获取

    public void get(){
            //获取数字: 年, 月, 日, 周几
            LocalDate nowDate = LocalDate.now();
            int year = nowDate.getYear(); //int year1 = nowDate.get(ChronoField.YEAR);
            int month = nowDate.getMonth().getValue();  //int month1 = nowDate.get(ChronoField.MONTH_OF_YEAR);
            int day = nowDate.getDayOfMonth();
            int week = nowDate.getDayOfWeek().getValue();
            System.out.println(year); //2020
            System.out.println(month); //2
            System.out.println(day);//25
            System.out.println(week);//2
    
            //获取数字: 时, 分, 秒
            LocalTime nowTime = LocalTime.now();
            int hour = nowTime.getHour(); //int hour1 = nowTime.get(ChronoField.HOUR_OF_DAY);
            int minute = nowTime.getMinute();  //int minute1 = nowTime.get(ChronoField.MINUTE_OF_HOUR);
            int second = nowTime.getMinute();  //int second1 = nowTime.get(ChronoField.SECOND_OF_MINUTE);
            System.out.println(hour);
            System.out.println(minute);
            System.out.println(second);
    
            //获取秒数
            Instant instant = Instant.now();
            long currentSecond = instant.getEpochSecond();
            long currentMilli = instant.toEpochMilli();
            long currentMilliSys = System.currentTimeMillis();
            System.out.println(currentSecond);
            System.out.println(currentMilli);
            System.out.println(currentMilliSys);
     }

    修改

    public void update(){
            //LocalDate、LocalTime、LocalDateTime、Instant为不可变对象,修改这些对象对象会返回一个副本
            //增加、减少年数、月数、天数等
    
            //plus
            LocalDate localDate = LocalDate.now();
            localDate = localDate.plusYears(1);  //加一年
            localDate = localDate.plusMonths(1);  //加一月
            localDate = localDate.plusDays(1);  //加一天
            localDate = localDate.plusWeeks(1);  //加一周
            localDate = localDate.minusMonths(1); //减少一个月
    
            //with 修改年为2019, 月为1
            localDate = localDate.withYear(2019);
            localDate = localDate.withMonth(1);
    }

    计算

    public void calculate(){
            //通过with计算: 这个月的第一天,最后一天、下个周末
            LocalDate localDate = LocalDate.now();
            LocalDate localDate1 = localDate.with(TemporalAdjusters.firstDayOfYear());
            LocalDate localDate2 = localDate.with(TemporalAdjusters.lastDayOfYear());
            LocalDate localDate3 =         
            localDate.with(TemporalAdjusters.next(DayOfWeek.SUNDAY));
            System.out.println(localDate1);
            System.out.println(localDate2);
            System.out.println(localDate3);
    }

    格式化

    public void format(){
            //格式化时间
            LocalDate localDate = LocalDate.of(2019, 9, 10);
            String s1 = localDate.format(DateTimeFormatter.BASIC_ISO_DATE);
            String s2 = localDate.format(DateTimeFormatter.ISO_LOCAL_DATE);
            //自定义格式化
            DateTimeFormatter dateTimeFormatter =   
            DateTimeFormatter.ofPattern("dd/MM/yyyy");
            String s3 = localDate.format(dateTimeFormatter);
    
            System.out.println(s1);
            System.out.println(s2);
            System.out.println(s3);
    }

    解析

    public void parse(){
            //解析时间  DateTimeFormatter是线程安全的
            LocalDate localDate1 = LocalDate.parse("20190910", 
            DateTimeFormatter.BASIC_ISO_DATE);
            LocalDate localDate2 = LocalDate.parse("2019-09-10", 
            DateTimeFormatter.ISO_LOCAL_DATE);
            System.out.println(localDate1);
            System.out.println(localDate2);
    }
  • 相关阅读:
    Redis订阅和发布模式和Redis事务
    Redis介绍和环境安装
    Redis基本数据类型
    MongoDB导入导出以及数据库备份
    MongoDB-python的API手记
    MongoDB对应SQL语句
    判断是否微信浏览器访问并得到微信版本号
    windows 下编译php扩展库pecl里的扩展memcache
    用PHPExcel类读取excel文件的内容
    用excel.php类库导出excel文件
  • 原文地址:https://www.cnblogs.com/smileblogs/p/12361962.html
Copyright © 2011-2022 走看看