0
点赞
收藏
分享

微信扫一扫

Java8新特性之新的时间日期API(一)

彭维盛 2022-04-18 阅读 60

使用LocalDateLocalTime、LocalDateTime、

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

注: ISO-8601 日历系统是国际标准化组织制定的现代公民的日期和时间的表示法

代码

package com.csdn.test;

import org.junit.Test;

import java.time.LocalDateTime;

/**
 * @author summer
 * @date 2022-04-18  15:54
 */
public class TestLocal {
    @Test
    public void test() {
        /**
         * 实际上LocaDate,LocalTime,LocalDateTime三者操作是一样的,这里
         * 就以LocalDateTime为例
         */

//        创建对象的方法
        LocalDateTime now = LocalDateTime.now();
        System.out.println(now);
        LocalDateTime of = LocalDateTime.of(2022, 4, 8, 15, 56, 22);
        System.out.println(of);

//        向当前对象上加上几天,几个月,几个小时,返回一个新的对象
        LocalDateTime localDateTime = of.plusDays(12);
        System.out.println(localDateTime);

        LocalDateTime localDateTime1 = of.plusHours(12);
        System.out.println(localDateTime1);

        LocalDateTime localDateTime2 = of.plusMinutes(22);
        System.out.println(localDateTime2);


//        向当前对象减去几天,几小时,几分钟则使用minus开头的方法,这里不再详细写

        /*
        将月份天数、年份天数、月份、年
        份修改为指 定 的 值 并 返 回 新 的
         对象

         withDayOfMonth,
         withDayOfYear,
         withMonth,
         withYear
         */

        LocalDateTime localDateTime3 = of.withDayOfMonth(6).withMonth(3).withYear(2012);
        System.out.println(localDateTime3);
        System.out.println(of);
//        获去对应的值
        System.out.println(of.getYear());
        System.out.println(of.getMonthValue());

       

    }
}

 Instant 时间戳

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

代码

package com.csdn.test;

import org.junit.Test;

import java.time.Instant;
import java.time.OffsetDateTime;
import java.time.ZoneOffset;

/**
 * @author summer
 * @date 2022-04-18  16:04
 */
public class TestInstant {
    @Test
    public void test(){
        Instant now = Instant.now();
        System.out.println(now);//2022-04-18T08:06:06.563Z

        OffsetDateTime dateTime = now.atOffset(ZoneOffset.ofHours(8));
        System.out.println(dateTime);//2022-04-18T16:06:06.563+08:00

        //毫秒数
        long milli = now.toEpochMilli();
        System.out.println(milli);
    }
}

Duration Period

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

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

代码

package com.csdn.test;

import org.junit.Test;

import java.time.*;

/**
 * @author summer
 * @date 2022-04-18  16:07
 */
public class DemoTest {
    @Test
    public void test() {
        Instant in1 = Instant.now();
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        Instant in2 = Instant.now();

        Duration between = Duration.between(in1, in2);
        System.out.println(between.toMillis());

        LocalDateTime l1 = LocalDateTime.now();
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        LocalDateTime l2 = LocalDateTime.now();

        long l = Duration.between(l1, l2).toMillis();
        System.out.println(l);


        LocalDate localDate1 = LocalDate.now();

        LocalDate localDate2 = LocalDate.of(2022,4,9);
        Period between1 = Period.between(localDate1, localDate2);
        System.out.println(between1.getDays());
    }
}

举报

相关推荐

0 条评论