Java8处理日期和时间

1.Instant 时间点

package com.company;

import java.time.Duration;
import java.time.Instant;

public class Main {

    public static void main(String[] args) {
        // Instant可表示的时间范围:±10亿年
        Instant min = Instant.MIN;
        Instant max = Instant.MAX;

        Instant start = Instant.now();
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        Instant end = Instant.now();
        // 计算两个时刻之间的时间量
        Duration timeElapsed = Duration.between(start, end);

        long millis = timeElapsed.toMillis();

        System.out.println("Instant的时间范围:" + min + "  至  " + max);
        System.out.println("时间差:" + millis);

    }
}

2.LocalDate 本地时间

// 构建LocalDate对象
LocalDate now = LocalDate.now();
LocalDate date1 = LocalDate.of(1903, 6, 14);
LocalDate date2 = LocalDate.of(1903, Month.JUNE, 14);
// 获取时间间隔
long until = date2.until(now, ChronoUnit.DAYS);
System.out.println(until);

3.Plus的用法

LocalDate now = LocalDate.now();
// 一年后
System.out.println(now.plusYears(1));
// 一月后
System.out.println(now.plusMonths(1));
// 一周后
System.out.println(now.plusWeeks(1));
// 一日后
System.out.println(now.plusDays(1));
// 2年8月3天后
// Period表示一段时间
System.out.println(now.plus(Period.of(2, 8, 3)));
System.out.println(now.plus(1, ChronoUnit.HALF_DAYS));

4.With的用法

LocalDate now = LocalDate.now();
// 使用with修改时间
System.out.println(now.withYear(2021));
System.out.println(now.with(ChronoField.YEAR, 2021));

3.TemporalAdjusters 日期调整器

LocalDate now = LocalDate.now();
// 下个月的第一天
System.out.println(now.with(TemporalAdjusters.firstDayOfNextMonth()));
// 下周周日
System.out.println(now.with(TemporalAdjusters.next(DayOfWeek.SUNDAY)));
// 上一个周日
System.out.println(now.with(TemporalAdjusters.previous(DayOfWeek.SUNDAY)));

4.自定义TemporalAdjuster

public class StartDayAdjuster implements TemporalAdjuster {
    @Override
    public Temporal adjustInto(Temporal temporal) {
        //Temporal是日期时间类对象的父接口,实际上可以理解为是LocalDate或者是LocalDateTime对象
        LocalDate localDate = LocalDate.from(temporal);
        if (localDate.getDayOfMonth() < 26) {
            return localDate.plusMonths(-1).withDayOfMonth(26);
        } else {
            return localDate.withDayOfMonth(26);
        }
    }
}

public class StartDayAdjuster implements TemporalAdjuster {
    @Override
    public Temporal adjustInto(Temporal temporal) {
        //Temporal是日期时间类对象的父接口,实际上可以理解为是LocalDate或者是LocalDateTime对象
        LocalDate localDate = LocalDate.from(temporal);
        if (localDate.getDayOfMonth() < 26) {
            return localDate.plusMonths(-1).withDayOfMonth(26);
        } else {
            return localDate.withDayOfMonth(26);
        }
    }
}

public static void main(String[] args) {
    LocalDate now = LocalDate.now();
    System.out.println(LocalDate.from(new StartDayAdjuster().adjustInto(now)));
    System.out.println(LocalDate.from(new EndDayAdjuster().adjustInto(now)));
}

5.query的使用

public class UtilDayQueryImpl implements TemporalQuery<Long> {
    @Override
    public Long queryFrom(TemporalAccessor temporal) {
        LocalDate now = LocalDate.from(temporal);
        LocalDate localDate51 = LocalDate.of(now.getYear(), 5, 1);
        if (now.isAfter(localDate51)) {
            localDate51 = localDate51.plusYears(1);
        }
        long days = ChronoUnit.DAYS.between(now, localDate51);
        return days;
    }
}

public static void main(String[] args) {
    LocalDate now = LocalDate.now();
    System.out.println(now.query(new UtilDayQueryImpl()));
}
posted @ 2020-05-08 16:05  xl4ng  阅读(303)  评论(0编辑  收藏  举报