Java8新特性(八)——时间日期API

八、时间日期API

1.LocalDate 、LocalTime 、LocalDateTime

LocalDate、LocalTime、LocalDateTime 类的实例是不可变的对象,分别表示使用 ISO-8601日历系统的日期、时间、日期和时间。它们提供了简单的日期或时间,并不包含当前的时间信息。也不包含与时区相关的信息。

常用方法:

方法描述
now()静态方法,根据当前时间创建对象
of()静态方法,根据指定日期/时间创建对象
plusDays, plusWeeks,plusMonths, plusYears向当前 LocalDate 对象添加几天、几周、几个月、几年
minusDays, minusWeeks,minusMonths, minusYears从当前 LocalDate 对象减去几天、几周、几个月、几年
plus, minus添加或减少一个 Duration 或 Period
withDayOfMonth,withDayOfYear,withMonth,withYear将月份天数、年份天数、月份、年份修改为指定 的值 并返回新的LocalDate 对象
getDayOfMonth获得月份天数(1-31)
getDayOfYear获得年份天数(1-366)
getDayOfWeek获得星期几(返回一个 DayOfWeek枚举值)
getMonth获得月份, 返回一个 Month 枚举值
getMonthValue获得月份(1-12)
getYear获得年份
until获得两个日期之间的 Period 对象,或者指定 ChronoUnits 的数字
isBefore, isAfter比较两个 LocalDate
isLeapYear判断是否是闰年

举例:

/**
     * LocalDate、LocalTime、LocalDateTime
     */
    @Test
    public void test1(){
        LocalDateTime ldt = LocalDateTime.now();
        System.out.println(ldt);

        LocalDateTime ldt1 = LocalDateTime.of(2019, 11, 7, 14, 41, 22,110000000);
        System.out.println(ldt1);

        LocalDateTime ldt2 = ldt.plusYears(1);
        System.out.println(ldt2);

        LocalDateTime ldt3 = ldt.minusMonths(1);
        System.out.println(ldt3);

        System.out.println(ldt.getYear());
        System.out.println(ldt.getMonth());
        System.out.println(ldt.getDayOfMonth());
        System.out.println(ldt.getHour());
        System.out.println(ldt.getMinute());
        System.out.println(ldt.getSecond());
    }

在这里插入图片描述

2.Instant时间戳

用于“时间戳”的运算。它是以Unix元年(传统的设定为UTC时区1970年1月1日午夜时分)开始所经历的描述进行运算

/**
     * Instant  时间戳
     */
    @Test
    public void test2() {
        //默认获取UTC时区,不是本机
        Instant instant1 = Instant.now();
        System.out.println(instant1);
        //时间偏移运算(与我们的时差为8)
        OffsetDateTime offsetDateTime = instant1.atOffset(ZoneOffset.ofHours(8));
        System.out.println(offsetDateTime);
        //以ms显示
        System.out.println(instant1.toEpochMilli());
        //当前时间戳代表时间
        Instant instant2 = Instant.ofEpochMilli(10000);
        System.out.println(instant2);
    }

在这里插入图片描述

3.Duration 和 Period

Duration:用于计算两个“时间”间隔

	/**
     * Duration 计算两个时间之间的间隔
     */
    @Test
    public void test3() {
        Instant instant1 = Instant.now();
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        Instant instant2 = Instant.now();
        Duration duration1 = Duration.between(instant1,instant2);
        System.out.println(duration1.toMillis());
        System.out.println("------------------------");
        LocalTime localTime1 = LocalTime.now();
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        LocalTime localTime2 = LocalTime.now();
        Duration duration2 = Duration.between(localTime1,localTime2);
        System.out.println(duration2.toMillis());
    }

在这里插入图片描述

Period:用于计算两个“日期”间隔

	/**
     * Period 计算两个日期之间的间隔
     */
    @Test
    public void test4() {
        LocalDate localDate1 = LocalDate.of(2020,10,1);
        LocalDate localDate2 = LocalDate.now();//2019-11-7
        Period period = Period.between(localDate2,localDate1);
        System.out.println(period.getYears());
        System.out.println(period.getMonths());
        System.out.println(period.getDays());
    }

在这里插入图片描述

3.时间校正器TemporalAdjuster

TemporalAdjuster : 时间校正器。有时我们可能需要获取例如:将日期调整到“下个周日”等操作。
TemporalAdjusters : 该类通过静态方法提供了大量的常用 TemporalAdjuster 的实现。

	/**
     * TemporalAdjuster : 时间校正器
     */
    @Test
    public void test5() {
        LocalDateTime localDateTime1 = LocalDateTime.now();
        System.out.println(localDateTime1);

        //调整到本月10号
        LocalDateTime localDateTime2 = localDateTime1.withDayOfMonth(10);
        System.out.println(localDateTime2);

        //调整到下个周日
        LocalDateTime localDateTime3 = localDateTime1.with(TemporalAdjusters.next(DayOfWeek.SUNDAY));
        System.out.println(localDateTime3);

        //自定义,下个工作日(3019-11-7下个工作日是8号)
        LocalDateTime localDateTime = localDateTime1.with(l -> {
            LocalDateTime localDateTime4 = (LocalDateTime) l;

            DayOfWeek dow = localDateTime4.getDayOfWeek();

            //周五加3天是工作日
            if (dow.equals(DayOfWeek.FRIDAY)) {
                return localDateTime4.plusDays(3);
                //周六加1天是工作日
            } else if (dow.equals(DayOfWeek.SATURDAY)) {
                return localDateTime4.plusDays(2);
                //其他都是加1天
            } else {
                return localDateTime4.plusDays(1);
            }
        });
        System.out.println(localDateTime);
    }

在这里插入图片描述

4.时间/日期格式化DateTimeFormatter

java.time.format.DateTimeFormatter 类,该类提供了三种格式化方法:

  • 预定义的标准格式
  • 语言环境相关的格式
  • 自定义的格式
	/**
     * DateTimeFormatter : 时间/日期格式化
     */
    @Test
    public void test6() {
        //按ISO_DATE对时间进行格式化
        DateTimeFormatter dtf = DateTimeFormatter.ISO_DATE;
        LocalDateTime ldt = LocalDateTime.now();
        String date = ldt.format(dtf);
        System.out.println(date);
        System.out.println("-------------------------");
        //按指定格式进行格式化
        DateTimeFormatter dtf2 = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH:mm:ss");
        String date2 = dtf2.format(ldt);
        System.out.println(date2);
        LocalDateTime parse = ldt.parse(date2, dtf2);
        System.out.println(parse);
    }

在这里插入图片描述

5.时区的处理

Java8 中加入了对时区的支持,带时区的时间为分别为:ZonedDate、ZonedTime、ZonedDateTime
其中每个时区都对应着 ID,地区ID都为 “{区域}/{城市}”的格式
例如 :Asia/Shanghai 等

ZoneId:该类中包含了所有的时区信息
getAvailableZoneIds() : 可以获取所有时区时区信息
of(id) : 用指定的时区信息获取 ZoneId 对象

	@Test
    public void test7() {
        //所有支持的时区
        Set<String> availableZoneIds = ZoneId.getAvailableZoneIds();
        availableZoneIds.forEach(System.out::println);
        //指定时区的时间
        LocalDateTime ldt = LocalDateTime.now(ZoneId.of("Asia/Tokyo"));
        System.out.println(ldt);
        LocalDateTime ldt2 = LocalDateTime.now();
        ZonedDateTime zonedDateTime = ldt2.atZone(ZoneId.of("Asia/Tokyo"));
        System.out.println(zonedDateTime);
    }

在这里插入图片描述

注:菜鸟一枚,才疏学浅,希望各位前辈能够批评指正,感谢感谢!!!

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值