JAVA8-时间测试,示例

2019-12-04  本文已影响0人  AlanSun2
package com.alan344.time;

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

/**
 * @author AlanSun
 * @date 2019/12/4 16:11
 */
public class TestTime {
    public static void main(String[] args) {
        System.out.println(MonthDay.now());
        System.out.println(LocalDate.now());
        System.out.println(System.currentTimeMillis());
        System.out.println(Instant.now().toEpochMilli());
        System.out.println(Instant.now().atOffset(ZoneOffset.ofHours(8)));
        System.out.println(LocalDateTime.ofInstant(Instant.now(), ZoneId.systemDefault()));

        System.out.println("---------------------------------------Duration-----------------------------------");
        // 间隔 Duration
        LocalDateTime ldt1 = LocalDateTime.of(LocalDate.now(), LocalTime.of(12, 20));
        LocalDateTime ldt2 = LocalDateTime.of(LocalDate.of(2018, 11, 4), LocalTime.of(12, 20, 20));
        Duration between = Duration.between(ldt1, ldt2);
        System.out.println(between.getSeconds());
        System.out.println(between.isZero());
        System.out.println(between.minus(Duration.ofSeconds(30)).getSeconds());
        System.out.println(between.minus(Duration.of(30, ChronoUnit.SECONDS)));
        System.out.println("toDays:" + between.toDays()); //换算成天,不足一天,则为0
        System.out.println("toHours:" + between.toHours());// 换算成小时,不足一小时,则为0
        System.out.println("negated:" + between.negated());// 取反

        // Period
        System.out.println("---------------------------------------period-----------------------------------");
        Period between1 = Period.between(ldt1.toLocalDate(), ldt2.toLocalDate());
        System.out.println(between1.getYears());//获取相差的年
        System.out.println(between1.getMonths());//获取相差的月
        System.out.println(between1.getDays());//获取相差的天
        System.out.println(between1.isNegative());// 是否是负数,一般按时间轴计算就是正的
        System.out.println(between1.negated()); // 年月日取反
        System.out.println("addTo:" + between1.negated().addTo(ldt1));
        System.out.println(between1.toTotalMonths());// 把年转化为月,计算总共差几个月
        System.out.println(between1.withDays(3));// 把原来的天替换掉
        System.out.println(between1);

        // 年月标准化:只有第三个是起作用的,因为它只对年月有效
        System.out.println(Period.ofDays(410).normalized().getDays());
        System.out.println(Period.ofWeeks(60).normalized().getMonths());
        System.out.println(Period.ofMonths(40).normalized().getMonths());

        System.out.println("------------------------------------------------zone---------------------");
        ZonedDateTime now = ZonedDateTime.now();
        System.out.println(now);
        System.out.println(now.getZone());
        System.out.println(now.getOffset());
        System.out.println(now.withMonth(11));
        System.out.println(now.with(ChronoField.MONTH_OF_YEAR, 9));
        System.out.println(now.with(MonthDay.of(3, 2)));
        System.out.println(now.with(TemporalAdjusters.dayOfWeekInMonth(2, DayOfWeek.MONDAY)));// 把年月日替换为当月第2周的星期一
        System.out.println(now.with(TemporalAdjusters.previous(DayOfWeek.MONDAY)));// 把年月日替换为这一周的周一
        System.out.println(now.with(TemporalAdjusters.next(DayOfWeek.MONDAY)));// 把年月日替换为下一周的周一
        System.out.println(now.with(TemporalAdjusters.ofDateAdjuster(localDate -> localDate.minusWeeks(1).with(ChronoField.DAY_OF_WEEK, DayOfWeek.SUNDAY.getValue()))));// 把年月日替换为上一周的周一
        System.out.println(now.minusWeeks(0).with(ChronoField.DAY_OF_WEEK, DayOfWeek.SUNDAY.getValue()));// 把年月日替换为上一周的周一
    }
}

Conclusion

  1. with 方法主要用于时间的替换。
  2. Period Duration 都是计算间隔,方法上也相识。Duration 更加通用一点,它能对时分秒进行计算;period 则只能对日期计算,它的好处是能计算相差多少个月。
上一篇 下一篇

猜你喜欢

热点阅读