12. Java8-日期API-LocalDate

x33g5p2x  于2021-12-18 转载在 其他  
字(7.9k)|赞(0)|评价(0)|浏览(428)

LocalDate 是Java8 中用于表示日期的时间类, 只表示年月日.

package org.zongf.learn.java8.date;

import org.junit.Test;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.*;

/** * @Description: 测试LocalDate 常用API * @author: zongf * @date: 2018-06-29 13:19 */
public class TestLocalDate {

    //测试构造方法
    @Test
    public void constructor(){

        // 获取当前日期
        LocalDate ld1 = LocalDate.now();    //2018-06-29

        // 根据指定年月日获取日期
        LocalDate ld2 = LocalDate.of(2018, 06, 29);

        // 根据字符串解析日期
        LocalDate ld5 = LocalDate.parse("2018-06-29");

        // 指定日期格式解析字符串
        LocalDate ld6 = LocalDate.parse("29/06/2018", DateTimeFormatter.ofPattern("dd/MM/yyyy"));

        // 获取某个时区的日期,下午13:36 测试时比北京日期晚一天
        LocalDate ld7 = LocalDate.now(ZoneId.of("US/Pacific"));  // 2018-06-28

        // 获取某年的第几天的日期
        LocalDate ld4 = LocalDate.ofYearDay(2018, 100);

        // 获取Unix 元年(1970-01-01) 之后的第几天
        LocalDate ld3 = LocalDate.ofEpochDay(100);

        System.out.println(ld1);
        System.out.println(ld2);
        System.out.println(ld3);
        System.out.println(ld4);
        System.out.println(ld5);
        System.out.println(ld6);
        System.out.println(ld7);

    }

    // 测试常用get 方法
    @Test
    public void test_get(){

        LocalDate today = LocalDate.now();

        System.out.println("年:" + today.getYear());
        System.out.println("月:" + today.getMonthValue());
        System.out.println("日:" + today.getDayOfMonth());
        System.out.println("月份(英文):" + today.getMonth());
        System.out.println("星期(英文):" + today.getDayOfWeek());

        System.out.println("当月第几天:" + today.getDayOfMonth());
        System.out.println("当年第几天:" + today.getDayOfYear());
        System.out.println("Unix元年后第几天:" + today.toEpochDay());
        System.out.println();

        System.out.println("当月总天数:" + today.lengthOfMonth());
        System.out.println("当年总天数:" + today.lengthOfYear());
        System.out.println("是否是闰年:" + today.isLeapYear());
        System.out.println();

        // 更通用的方法
        System.out.println("年:" + today.get(ChronoField.YEAR));
        System.out.println("月:" + today.get(ChronoField.MONTH_OF_YEAR));
        System.out.println("日:" + today.get(ChronoField.DAY_OF_MONTH));
        System.out.println("本周第几天(数字):" + today.get(ChronoField.DAY_OF_WEEK));
        System.out.println("当年第几天:" + today.get(ChronoField.DAY_OF_YEAR));
        System.out.println("当月第几天%7:" + today.get(ChronoField.ALIGNED_DAY_OF_WEEK_IN_MONTH));
        System.out.println("当年第几天%7" + today.get(ChronoField.ALIGNED_DAY_OF_WEEK_IN_YEAR));
        System.out.println("当月第几周:" + today.get(ChronoField.ALIGNED_WEEK_OF_MONTH));
        System.out.println("当年第几周:" + today.get(ChronoField.ALIGNED_WEEK_OF_YEAR));
        System.out.println("Unix元年后第几天:" + today.getLong(ChronoField.EPOCH_DAY));

    }

    // 测试常用日期运算
    @Test
    public void test_plus(){

        LocalDate today = LocalDate.now();

        System.out.println("当前日期:" + today);
        System.out.println();

        // 使用plus 获取当前日期
        System.out.println("三天后日期:" + today.plusDays(3));
        System.out.println("三月后日期:" + today.plusMonths(3));
        System.out.println("三周后日期:" + today.plusWeeks(3));
        System.out.println("三年后日期:" + today.plusYears(3));
        System.out.println();

        System.out.println("三天前日期:" + today.minusDays(3));
        System.out.println("三月前日期:" + today.minusMonths(3));
        System.out.println("三周前日期:" + today.minusWeeks(3));
        System.out.println("三年前日期:" + today.minusYears(3));
        System.out.println();

        System.out.println("三天前日期:" + today.minus(Period.ofDays(3)));
        System.out.println("三月前日期:" + today.minus(Period.ofMonths(3)));
        System.out.println("三年前日期:" + today.minus(Period.ofYears(3)));
        System.out.println("三周前日期:" + today.minus(Period.ofWeeks(3)));
        System.out.println("三年零三个月又三天前日期:" + today.minus(Period.of(3,3,3)));
        System.out.println();

        System.out.println("三天后日期:" + today.plus(Period.ofDays(3)));
        System.out.println("三月后日期:" + today.plus(Period.ofMonths(3)));
        System.out.println("三年后日期:" + today.plus(Period.ofYears(3)));
        System.out.println("三周后日期:" + today.plus(Period.ofWeeks(3)));
        System.out.println("三年零三个月又三天后日期:" + today.plus(Period.of(3,3,3)));
    }

    // 测试日期比较
    @Test
    public void test_compare(){
        LocalDate today = LocalDate.now();
        LocalDate tomorrow = LocalDate.now().plusYears(1);
        LocalDate yesterday = LocalDate.now().minusYears(1);

        System.out.println(today.isBefore(tomorrow));
        System.out.println(today.isAfter(tomorrow));
        System.out.println(today.compareTo(yesterday));
        System.out.println(yesterday.compareTo(tomorrow));

    }

    // 使用with 方法获取和当前日期相关的日期
    @Test
    public void with(){
        LocalDate ld = LocalDate.now();
        System.out.println("当期日期:" + ld);
        System.out.println("替换年为2020:" + ld.withYear(2020));
        System.out.println("替换月为5:" + ld.withMonth(5));
        System.out.println("替换日为15:" + ld.withDayOfMonth(15));
        System.out.println("替换为当年第100天:" + ld.withDayOfYear(100));
        System.out.println();

        System.out.println("获取当月第一天:" + ld.with(TemporalAdjusters.firstDayOfMonth()));
        System.out.println("获取当年第一天:" + ld.with(TemporalAdjusters.firstDayOfYear()));
        System.out.println("获取下月第一天:" + ld.with(TemporalAdjusters.firstDayOfNextMonth()));
        System.out.println("获取来年第一天:" + ld.with(TemporalAdjusters.firstDayOfNextYear()));
        System.out.println("获取当年最后一天:" + ld.with(TemporalAdjusters.lastDayOfYear()));
        System.out.println("获取当月最后一天:" + ld.with(TemporalAdjusters.lastDayOfMonth()));
        System.out.println("获取当月第一个周五:" + ld.with(TemporalAdjusters.firstInMonth(DayOfWeek.FRIDAY)));
        System.out.println("获取当月最后一个周五:" + ld.with(TemporalAdjusters.lastInMonth(DayOfWeek.FRIDAY)));
        System.out.println("获取当月下一个周五(不包括今天):" + ld.with(TemporalAdjusters.next(DayOfWeek.FRIDAY)));
        System.out.println("获取当月下一个周五(包括今天):" + ld.with(TemporalAdjusters.nextOrSame(DayOfWeek.FRIDAY)));
        System.out.println("获取当月上一个周五(不包括今天):" + ld.with(TemporalAdjusters.previous(DayOfWeek.FRIDAY)));
        System.out.println("获取当月上一个周五(包括今天):" + ld.with(TemporalAdjusters.previousOrSame(DayOfWeek.FRIDAY)));
        // 自定义TemporalAdjuster 对象,实现下一个工作日
        TemporalAdjuster nextWorkingDay = TemporalAdjusters.ofDateAdjuster(
                temporal -> {
                    DayOfWeek dow = DayOfWeek.of(temporal.get(ChronoField.DAY_OF_WEEK));
                    int dayToAdd = 1;
                    if(dow==DayOfWeek.FRIDAY) dayToAdd = 3;
                    if(dow==DayOfWeek.SATURDAY) dayToAdd = 2;
                    return temporal.plus(dayToAdd, ChronoUnit.DAYS);
                }
        );
        System.out.println("获取下一个工作日:" + ld.with(nextWorkingDay));
    }

    // 计算两个时间之间的差
    @Test
    public void test_until(){
        LocalDate ld1 = LocalDate.of(2005, 9, 30);
        LocalDate ld2 = LocalDate.of(2018, 3, 10);

        System.out.println("日期间隔-总年数:" + ld1.until(ld2, ChronoUnit.YEARS));
        System.out.println("日期间隔-总月数:" + ld1.until(ld2, ChronoUnit.MONTHS));
        System.out.println("日期间隔-总天数:" + ld1.until(ld2, ChronoUnit.DAYS));
        System.out.println("时间间隔-多少个十年:" + ld1.until(ld2, ChronoUnit.DECADES));
    }

    // 测试日期直接的间隔
    @Test
    public void test_between(){

        LocalDate ld1 = LocalDate.of(2015, 8, 28);
        LocalDate ld2 = LocalDate.of(2018, 3, 10);

        // 计算日期之间相差总时间数

        Period period = Period.between(ld1, ld2);
// Period period = ld1.until(ld2); // 等价于
        System.out.println("日期间隔:" + period);
        System.out.println("日期间隔-年:" + period.getYears());
        System.out.println("日期间隔-月:" + period.getMonths());
        System.out.println("日期间隔-日:" + period.getDays());
        System.out.println("日期间隔-总月数:" + period.toTotalMonths());

        System.out.println("日期间隔-年:" + period.get(ChronoUnit.DAYS));
        System.out.println("日期间隔-月:" + period.get(ChronoUnit.MONTHS));
        System.out.println("日期间隔-日:" + period.get(ChronoUnit.YEARS));

        System.out.println("日期间隔是否是负数:" + period.isNegative());
        System.out.println("日期间隔是否是0:" + period.isZero());

    }

    // 测试日期转换
    @Test
    public void test_converter(){

        LocalDate ld = LocalDate.now();

        // 获取当天开始时间
        LocalDateTime ldt1 = ld.atStartOfDay();
        System.out.println(ldt1);

        // 获取指定时区当天开始时间
        ZonedDateTime zonedDateTime = ld.atStartOfDay(ZoneId.of("US/Pacific"));
        System.out.println(zonedDateTime); //2018-06-30T00:00-07:00[US/Pacific]

        // 获取当天具体时间
        LocalDateTime ldt2 = ld.atTime(8, 20, 30);
        System.out.println(ldt2);

        // 获取当天具体时间
        LocalDateTime ldt3 = ld.atTime(LocalTime.of(8, 20, 30));
        System.out.println(ldt3);

    }

}

相关文章