日期时间处理

Java8时间日期API

package time;

import org.junit.Test;

import java.time.*;
import java.time.temporal.ChronoField;
import java.time.temporal.ChronoUnit;
import java.time.temporal.TemporalField;

public class DateTest {

    /**
     * LocalDate 本地日期
     * LocalTime 本地时间
     * LocalDateTime 本地日期时间
     */
    @Test
    public void testLocalDateTime() {
        // 1. LocalDate 用于表示一个简单的日期,不包含时间信息以及时区信息
        // 获取当前日期的LocalDate实例
        LocalDate localDate = LocalDate.now();
        // 创建一个指定日期的LocalDate实例
        localDate = LocalDate.of(2018, 1, 2); // 2018年1月2日
        // 获取日期的一些信息
        int year = localDate.getYear();// 年
        System.out.println(year); // 2018
        Month month = localDate.getMonth(); // 月
        System.out.println(month.getValue()); // 1
        int monthValue = localDate.getMonthValue(); // 月
        System.out.println(monthValue); // 1

        // 2. LocalTime 用于表示一个简单时间, 基本使用和LocalDate类似
        LocalTime localTime = LocalTime.of(23, 34, 28, 899); // 时间小时数 0~23 分钟/秒 0~59 毫秒 0~999
        System.out.println(localTime.getHour()); // 23
        System.out.println(localTime.getMinute()); // 34
        System.out.println(localTime.getSecond()); // 28
        System.out.println(localTime.getNano()); // 899

        // 3. 使用 TemporalField 获取时间字段
        // for example
        int dayOfMonth = localDate.get(ChronoField.DAY_OF_MONTH);
        System.out.println(dayOfMonth); // 2

        int minuteOfHour = localTime.get(ChronoField.MINUTE_OF_HOUR);
        System.out.println(minuteOfHour); // 34

        // 如果 TemporalField 不符合实例,比如
        // localDate.get(ChronoField.HOUR_OF_DAY);
        // 就会抛出 java.time.temporal.UnsupportedTemporalTypeException Unsupported field: HourOfDay

        // 4. LocalDate 可以表示日期
        // LocalTime 可以表示时间
        // LocalDateTime 既可以表示日期,又可以表示时间, LocalDateTime内部依赖了两个对象:LocalDate 和 LocalTime

        // 创建LocalDateTime对象,通过合并日期时间的方式
        LocalDateTime localDateTime = LocalDateTime.of(localDate, localTime);
        // 通过指定日期时间方式创建
        localDateTime = LocalDateTime.of(2018, 1, 2, 23, 34, 28, 899);
        // 通过atTime 和 atDate方法 合并创建
        localDateTime = localDate.atTime(localTime);
        localDateTime = localDate.atTime(23, 34, 28, 899);
        localDateTime = localTime.atDate(localDate);

        // 5. 拆分日期时间
        // 拆分为日期
        LocalDate localDate1 = localDateTime.toLocalDate();
        // 拆分为时间
        LocalTime localTime1 = localDateTime.toLocalTime();
    }

    /**
     * 表示时间线上的瞬时点:时间戳
     */
    @Test
    public void testInstant() {
        // instant 瞬时,瞬间
        // Instant 用于表示某个时间点,不同于Date对象精确到毫秒,他精确到纳秒级别
        // 即时间戳
        Instant now = Instant.now();
        System.out.println(now); // 当前时间戳 2018-10-23T07:04:51.662Z
        System.out.println(now.getNano()); // 当前时间戳 662000000
    }

    /**
     * Period 基于日期的时间量 比如2个月15天
     * Duration 基于时间的时间量 比如 34.5秒
     *
     * 时间量:时间间隔,量词
     */
    @Test
    public void testPeriodDuration() {

        // 时间间隔输出时,表示方式为 ISO-8601 表示(国际化标准组织对时间的表示方法)
        // P 代表时间间隔
        // 如表示时间间隔 一年三个月五天六小时七分三十秒 可以表示为 P1Y3M5DT6H7M30S 其中T是日期与时间合并时,时间表示开始的标识

        // Duration的创建
        // 比如,time1和time2 的时间间隔
        LocalTime time1 = LocalTime.of(10, 10, 10);
        LocalTime time2 = LocalTime.of(20, 20, 20);
        Duration between = Duration.between(time1, time2);
        System.out.println(between); // PT10H10M10S 时间间隔为 是个小时10分钟10秒

        LocalDate date1 = LocalDate.of(2018, 6, 20);
        LocalDate date2 = LocalDate.of(2018, 1, 1);
        Period period = Period.between(date1, date2);
        System.out.println(period); // P-5M-19D 时间间隔为5个月 19天

        // 直接创建日期间隔
        Period p3m = Period.ofMonths(3); // 3个月
        System.out.println(p3m);
        // 使用字符串创建 一年三个月五天
        Period p3m1 = Period.parse("P1Y3M5D");
        System.out.println(p3m1);

        // 创建一个三分钟间隔
        Duration pt3m = Duration.ofMinutes(3);
        System.out.println(pt3m);
        // 或者
        pt3m = Duration.of(3, ChronoUnit.MINUTES);
        // 创建一个 六小时七分三十秒的 间隔
        Duration pt3m1 = Duration.parse("PT6H7M30S");// 由一个临时时间点创建 interval
        System.out.println(pt3m1);

    }
}

Date 与 LocalDate 和 LocalDateTime 互转

Date date = new Date();
// Date -> LocalDate
LocalDate localDate = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
// Date -> LocalTime
LocalTime localTime = date.toInstant().atZone(ZoneId.systemDefault()).toLocalTime();
// Date -> LocalDateTime
LocalDateTime localDateTime = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
// localDate -> Date
date = Date.from(localDate.atStartOfDay(ZoneId.systemDefault()).toInstant());
// localDateTime -> Date
date = Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());

最后更新于