ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

Java8新特性 -- 时间类

2021-12-26 22:02:01  阅读:158  来源: 互联网

标签:12 -- System 特性 LocalDateTime println now Java8 out


import org.junit.Test;

import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.Temporal;
import java.time.temporal.TemporalAdjuster;

/**
 * @description: 验证JDK8的日期和时间类
 * @author: xxx
 * @date: 2021/12/02 22:08
 */
public class TestLocalDateTime {
    /**
    * 验证:LocalDate 日期 yyyy-MM-dd
    **/
    @Test
    public void testLocalDate(){
        LocalDate.of(1985, 8, 6);
        LocalDate.of(1985, Month.AUGUST,8);
        LocalDate.ofYearDay(1985,218);
        //从1970年之后的N天的日期
        LocalDate.ofEpochDay(1);

        LocalDate now = LocalDate.now();
        now.getYear();
        now.getMonth().getValue();
        now.getDayOfMonth();
        now.getDayOfWeek();
        now.getDayOfYear();
    }

    /**
    * 验证:LocalTime 时间 HH:mm:ss(.ns)
    **/
    @Test
    public void testLocalTime(){
        LocalTime.of(11, 15, 12, 234_444_933);
        LocalTime.ofSecondOfDay(100L);
        LocalTime.of(1, 1, 1);

        LocalTime now = LocalTime.now();
        now.getHour();
        now.getMinute();
        now.getSecond();
        now.getNano();
    }

    /**
    * 验证:LocalDateTime 日期时间 yyyy-MM-ddTHH:mm:ss(.ns)
    **/
    @Test
    public void testLocalDateTime(){
        LocalDateTime.of(1908, 12, 12, 12, 12, 12, 12);
        LocalDateTime now = LocalDateTime.now();
        now.getYear();
        now.getMonthValue();
        now.getDayOfMonth();
        now.getDayOfYear();
        now.getDayOfWeek();
        now.getHour();
        now.getMinute();
        now.getSecond();
        now.getNano();
    }

    /**
    * 验证:日期时间的操作
     * 日期时间对象都是final修饰,不可变
    **/
    @Test
    public void testLocalDateTimeOption(){
        //修改日期时间 不对原始对象做修改 withAttribute
        LocalDateTime now = LocalDateTime.now();
        LocalDateTime setYear = now.withYear(1993);
        System.out.println(now);
        System.out.println(setYear);

        //在当前对象基础上加上or减去指定时间  plus | minus
        now.plusDays(2);
        now.plusMonths(2);
        now.minusYears(1);

        //日期比较 isBefore() isAfter() equal()
        LocalDate of = LocalDate.of(1998, 12, 12);
        System.out.println(LocalDate.now().isAfter(of));

        //日期格式化,默认格式 yyyy-MM-ddTHH:mm:ss
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        System.out.println(now.format(formatter));

        System.out.println(LocalDateTime.parse("1993-12-12 12:12:12", formatter));
        System.out.println(LocalDateTime.parse("1993-12-12T12:12:12"));

        /**
         * 日期时间差
         *      Duration: LocalTime之间的距离对象
         *      Period: LocalDate之间的距离对象
         */
        Duration duration = Duration.between(LocalTime.of(18, 12, 12), LocalTime.now());
        System.out.println(duration.toDays());
        System.out.println(duration.toHours());
        System.out.println(duration.toMinutes());

        //10天 = 240H
        System.out.println(Duration.ofDays(10));
        //1H
        System.out.println(Duration.ofHours(1));
        //6M
        System.out.println(Duration.ofMinutes(6));
        //10S
        System.out.println(Duration.ofSeconds(10));

        Period period = Period.between(LocalDate.now(), LocalDate.now().plusDays(1000));
        System.out.println(period.getYears());
        System.out.println(period.getMonths());
        System.out.println(period.getDays());
    }


    /**
    * 验证:Instant类 -- 时间戳
    **/
    @Test
    public void testInstant(){
        Instant now = Instant.now();
        System.out.println(now);

        //epoch 到现在的秒
        System.out.println(now.getEpochSecond());
        //纳秒数指的是该秒内的纳秒时间
        System.out.println(now.getNano());

        //epoch 到现在的 毫秒
        System.out.println(now.toEpochMilli());
        //等同于toEpochMilli
        System.out.println(System.currentTimeMillis());

        //epoch 之后的5秒
        System.out.println(Instant.ofEpochSecond(5));
    }

    /**
    * 验证:时间校正器
    **/
    @Test
    public void testTimeTemporalAdjuster(){
        LocalDateTime now = LocalDateTime.now();
        //下个月的第一天
        TemporalAdjuster temporalAdjuster = new TemporalAdjuster() {
            @Override
            public Temporal adjustInto(Temporal temporal) {
                //这里temporal = now()
                LocalDateTime now = (LocalDateTime)temporal;
                System.out.println(now);
                LocalDateTime nextMonth = now.plusMonths(1).withDayOfMonth(1);
                return nextMonth;
            }
        };
        System.out.println(now.with(temporalAdjuster));
    }

    /**
    * 验证:时区
     *      LocalDate/DateTime/Time 不带时区
     *      ZoneDate/DateTime/Time  带时区
    **/
    @Test
    public void testTimeZone(){
        //获取所有的时区ID,格式为  区域/城市
        //ZoneId.getAvailableZoneIds().forEach(System.out::println);

        //根据当前系统的时区now(Clock.systemDefaultZone()),返回时间
        System.out.println(LocalDateTime.now());

        //获取带时区的类,世界标准时间
        System.out.println(ZonedDateTime.now(Clock.systemUTC()));
        System.out.println(ZonedDateTime.now());

        //指定时区
        System.out.println(ZonedDateTime.now(ZoneId.of("America/Vancouver")));
    }

}

标签:12,--,System,特性,LocalDateTime,println,now,Java8,out
来源: https://www.cnblogs.com/jiangnanxidao/p/15639858.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有