Java--8--新特性--新的日期API

LocalDate、LocalTime、LocalDateTime 类的实 例是不可变的对象,分别表示使用 ISO-8601日 历系统的日期、时间、日期和时间。

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

直接来看代码吧。

 同时新的日期API 也解决了旧日期API在线程中的问题,

package NewTimeP;

import org.junit.Test;

import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;

public class NewTime {

    public void test() {
        //LocalDate  LocalTime LocalDateTime
        //上面三个是我们可以看得懂的时间,其操作方法是一样的,这里只写一下LocalDateTime的使用方法
        LocalDateTime localDateTime = LocalDateTime.now();//得到现在的时间
        System.out.println("localDateTime = " + localDateTime);
        //localDateTime = 2017-11-30T09:28:23.564

        //按照指定的时间创建新的对象
        LocalDateTime of = LocalDateTime.of(2016, 12, 12, 12, 12, 12, 23);
        System.out.println("of = " + of);
        //of = 2016-12-12T12:12:12.000000023

        //在原有基础上加上两年,其他操作时一样的,区别一下LocalDate和LocalTime,他们是各自分别只管年月日和时分秒,所以对应的就只有操作年月日或时分秒的方法
        LocalDateTime localDateTime1 = of.plusYears(2);
        System.out.println("localDateTime1 = " + localDateTime1);
        //localDateTime1 = 2018-12-12T12:12:12.000000023

        //在原有基础上减去两年
        LocalDateTime localDateTime2 = of.minusYears(2);
        System.out.println("localDateTime2 = " + localDateTime2);
        //localDateTime2 = 2014-12-12T12:12:12.000000023

        //得到各种年月日时分秒
        System.out.println(localDateTime.getYear());
        System.out.println(localDateTime.getMonth());//这里是得到了Month的对象,可以对其进行操作的
        System.out.println(localDateTime.getMonthValue());//这里就只是返回一个int值
        System.out.println(localDateTime.getDayOfMonth());
        System.out.println(localDateTime.getDayOfWeek());
        System.out.println(localDateTime.getHour());
        System.out.println(localDateTime.getMinute());
        System.out.println(localDateTime.getSecond());
    }

    public void test1() {
        //Instant 时间戳,以1970年一月一日00:00:00到指定时间的毫秒值
        Instant instant = Instant.now();//默认获取的是UTC时区时间,世界协调时间,
        System.out.println(instant);//2017-11-30T01:36:45.315Z

        //由于UTC时区与我们的现在的时间相差八个小时,那么我们如果想得到现在的时间就必须做一下偏移量的运算。
        //下面的意思是把当前的UTC时区的时间做了八个小时的偏移量运算后返回一个偏移量的时间对象
        OffsetDateTime offsetDateTime = instant.atOffset(ZoneOffset.ofHours(8));
        System.out.println("offsetDateTime = " + offsetDateTime);//offsetDateTime = 2017-11-30T09:38:59.003+08:00

        //得到毫秒值
        System.out.println(instant.toEpochMilli());//1512006123574

        //我们可以看到结果 是在元年上进行添加的操作的!下面的意思是在1970年1月1日00:00:00上加了一秒!
        Instant instant1 = Instant.ofEpochSecond(1);
        System.out.println("instant1 = " + instant1);//instant1 = 1970-01-01T00:00:01Z
    }

    public void test2() throws InterruptedException {
        //Duration:计算两个时间之间的间隔的
        Instant now = Instant.now();
        Thread.sleep(500);
        Instant now1 = Instant.now();
        Duration between = Duration.between(now, now1);
        System.out.println(between.toMillis());//500  这里需要提醒的是 Duration中的得到各种时间的方法是不一样的,有to啥啥啥还有 get啥啥啥,自己找找吧

        LocalTime localtime = LocalTime.now();
        Thread.sleep(50);
        LocalTime now2 = LocalTime.now();
        System.out.println(Duration.between(localtime, now2).toMillis());//51  在这的到了Duration也可以进行相加减日期的操作!
    }

    public void test3() throws InterruptedException {

        //Period计算两个日期之间的间隔
        LocalDate of = LocalDate.of(2015, 1, 1);
        LocalDate now = LocalDate.now();
        Period between = Period.between(of, now);
        System.out.println("between = " + between);//between = P2Y10M29D  表示P 两年Y十个月M二十九天D
        System.out.println("between = " + between.getYears());//between = 2
        System.out.println("between = " + between.getDays());//between = 29
        System.out.println("between = " + between.toTotalMonths());//相差三十四个月   between = 34

    }

    public void test4() {
        //TemporalAdjuster  时间校正器
        LocalDateTime localDateTime = LocalDateTime.now();
        System.out.println("localDateTime = " + localDateTime);//localDateTime = 2017-11-30T10:01:58.836

        //指定这个月中的天数
        LocalDateTime localDateTime1 = localDateTime.withDayOfMonth(10);
        System.out.println("localDateTime1 = " + localDateTime1);//localDateTime1 = 2017-11-10T10:01:58.836
        //指定一年中的第几天  那就是 一月一号啊
        LocalDateTime localDateTime2 = localDateTime.withDayOfYear(1);
        System.out.println("localDateTime2 = " + localDateTime2);//localDateTime2 = 2017-01-01T10:01:58.836

        //下一个周日
        LocalDateTime with = localDateTime.with(TemporalAdjusters.next(DayOfWeek.SUNDAY));
        System.out.println("with = " + with);//with = 2017-12-03T10:08:23.938

        //自定义校正器
        //with里面是一个函数式接口,这时候就可以使用Lambda表达式
        LocalDateTime with1 = localDateTime.with((e) -> {
            LocalDateTime e1 = (LocalDateTime) e;//强转一下
            DayOfWeek dayOfWeek = e1.getDayOfWeek();//获取今天是周几
            if (dayOfWeek.equals(DayOfWeek.FRIDAY)) {//如果是周五的话,那么就加上三天,加上后就是下周周一的日期
                return e1.plusDays(3);
            } else if (dayOfWeek.equals(DayOfWeek.THURSDAY)) {//如果是周四的话,那么就加上四天,加上后就是下周周一的日期
                return e1.plusDays(4);
            }
            return null;//其实不应该这么返回的,懒得写了,
        });
        System.out.println(with1);//2017-12-04T10:17:05.773     周一周一
    }

    public void test5() {
        //DateTimeFormatter 格式化日期与时间的。
        //在这个类中其实已经提供了好多已经定义好的格式化标准
        DateTimeFormatter isoDate = DateTimeFormatter.ISO_DATE;

        LocalDateTime dateTime = LocalDateTime.now();

        String format = isoDate.format(dateTime);
        System.out.println("format = " + format);//format = 2017-11-30

        //也可以自己定义
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
        String format1 = dateTimeFormatter.format(dateTime);
        System.out.println(format1);//20171130103756

        //当然如果我们拿到了特定的日期时间格式也可以解析出原来的时间;
        //意思就是用一个实例对象去调用parse方法,第一个参数为日期字符串,第二个就是解析的格式
        LocalDateTime parse = LocalDateTime.now().parse(format1, dateTimeFormatter);
        System.out.println(parse);//2017-11-30T10:40:39
    }
    @Test
    public void test6(){
        //时区 ZonedDate ZonedTime  ZonedDateTime
        LocalDateTime now = LocalDateTime.now(ZoneId.of("Asia/Shanghai"));
        System.out.println("now = " + now);//now = 2017-11-30T10:49:40.584

        LocalDateTime now1 = LocalDateTime.now();
        ZonedDateTime zonedDateTime = now1.atZone(ZoneId.of("Asia/Shanghai"));
        System.out.println("zonedDateTime = " + zonedDateTime);//zonedDateTime = 2017-11-30T10:51:02.094+08:00[Asia/Shanghai]
    }
}