Java8时间的简单时间

package com.java8.date;

import org.junit.Test;

import java.text.SimpleDateFormat;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.*;

public class DateTest {

    @Test
    public void LocalDateTest() {

        // of方法获取一个指定日期的LocalDate
        LocalDate date1 = LocalDate.of(2018, 12, 29);
        System.out.println(date1);
        System.out.println(date1.getYear());
        System.out.println(date1.getMonth());

        System.out.println(date1.getMonthValue());
        System.out.println(date1.getDayOfMonth());
        System.out.println(date1.getDayOfWeek());
        System.out.println(date1.getDayOfWeek().getValue());
        System.out.println(date1.getDayOfYear());

        System.out.println("判断时间前后关系:" + date1.isBefore(date1));

        // now 获取当前时间
        System.out.println(LocalDate.now());

        //  获取指定字段的值
        System.out.println(date1.get(ChronoField.YEAR));
        System.out.println(date1.get(ChronoField.MONTH_OF_YEAR));
        System.out.println(date1.get(ChronoField.DAY_OF_YEAR));
        System.out.println(date1.get(ChronoField.DAY_OF_MONTH));

        // 多了一些加减运算
        //Peroid是针对日期的 , Duration 主要是针对Time的
        System.out.println(date1.minus(Period.ofYears(2)));
        System.out.println(date1.minus(Period.ofDays(2)));
        System.out.println(date1.minus(Period.ofWeeks(2)));

        Period between = Period.between(date1.minus(Period.ofYears(2)), date1);
        Period between2 = Period.between(date1.minus(Period.ofMonths(2)), date1);

        System.out.println(between.getMonths());
        System.out.println(between2.getMonths());
        System.out.println("date1.minus(between) = " + date1.minus(between));


        LocalDate now = LocalDate.now();
        // 替换年
        System.out.println(now.withYear(2016));

        // 计算某一个时间字段的取值范围
        System.out.println(now.range(ChronoField.DAY_OF_MONTH));
    }


    @Test
    public void LocalTimeTest() {

        LocalTime now = LocalTime.now();
        System.out.println(now);

        LocalTime time = LocalTime.of(12, 12);
        System.out.println(time);

        System.out.println(time.isBefore(now));
        // 由于是time所以不支持年字段
        System.out.println(time.isSupported(ChronoField.YEAR));
        System.out.println(time.isSupported(ChronoUnit.YEARS));
        System.out.println(time.isSupported(ChronoUnit.HOURS));


        System.out.println(time.format(DateTimeFormatter.ofPattern("hh:mm:ss")));

        // Duration 主要是针对Time的,Peroid是针对日期的
        System.out.println(" time.minus(Duration.ofHours(2)) = " + time.minus(Duration.ofHours(2)));
        System.out.println(" time.plus(Duration.ofHours(2)) = " + time.plus(Duration.ofHours(2)));


    }

    @Test
    public void LocalDateTimeTest() {

        LocalDateTime dateTime = LocalDateTime.now();
        //2018-12-29T13:38:03.212
        System.out.println(dateTime.toString());

        dateTime = LocalDateTime.of(2018, 12, 29, 12, 12);

        System.out.println(dateTime);

        //指定今天的12点12分

        LocalDateTime toDay1212 = LocalDate.now().atTime(LocalTime.of(12, 12));
        System.out.println(toDay1212);

        //DateTime转Date
        System.out.println(toDay1212.toLocalDate());
        //DateTime转Time
        System.out.println(toDay1212.toLocalTime());
        // 替换某一个时间单位
        System.out.println(toDay1212.withHour(14));

    }


    @Test
    public void instantTest() {

        System.out.println(Instant.now());
        System.out.println(System.currentTimeMillis());
        System.out.println(System.nanoTime());
        System.out.println(Instant.now().toEpochMilli());
        System.out.println(Instant.now().plusNanos(0));
        System.out.println(Instant.now().get(ChronoField.NANO_OF_SECOND));


        Period between = Period.between(LocalDate.of(2018, 12, 26), LocalDate.of(2018, 12, 28));

        System.out.println(between.getDays());
        // between表示是一个间隔,做加减法时候只拿间隔做运算,不考虑具体的起止日期
        System.out.println(LocalDateTime.now().plus(between));

    }


    @Test
    public void TemporalAdjusterTest() {


        // 计算当前日期的加一个周五
        System.out.println(LocalDate.now().with(TemporalAdjusters.nextOrSame(DayOfWeek.FRIDAY)));
        // 计算该月的最后一天
        System.out.println(LocalDate.now().with(TemporalAdjusters.lastDayOfMonth()));
        // 计算该年的最后一天
        System.out.println(LocalDate.now().with(TemporalAdjusters.lastDayOfYear()));


    }


    @Test
    public void DateTimeFormatterTest() {
        //DateTimeFormatter 线程安全线的 ,SimpleDateFormat线程不安全原因是底层公用一个Calender成员
        System.out.println(LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));


    }


    @Test
    public void ZoneIdTest() {

        System.out.println(ZoneId.systemDefault());

    }
}

  

原文地址:https://www.cnblogs.com/leodaxin/p/10195907.html