首页 > Windows开发 > 详细

Jdk8新特性 【时间】 api LocalDate,LocalTime,LocalDateTime

时间:2020-02-25 15:54:54      阅读:75      评论:0      收藏:0      [点我收藏+]

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);
}

 

Jdk8新特性 【时间】 api LocalDate,LocalTime,LocalDateTime

原文:https://www.cnblogs.com/smileblogs/p/12361962.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!