14. Java8-日期API-LocalDateTime

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

LocalDateTime 是java8 中用于表示日期与时间的类.

import org.junit.Test;

import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoField;
import java.time.temporal.ChronoUnit;
import java.time.temporal.TemporalAdjuster;
import java.time.temporal.TemporalAdjusters;

/** * @Description: * @author: zongf * @date: 2018-06-29 16:34 */
public class TestLocalDateTime {

    // 创建时间对象
    @Test
    public void test_constructor(){

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

        // 获取指定时区当前时间
        System.out.println(LocalDateTime.now(ZoneId.of("US/Pacific")));

        // 使用具体年月日时分秒构建时间, 存在多个重载方法
        System.out.println(LocalDateTime.of(2018, 06 ,30, 15, 30, 40));

        // 解析字符串构造时间
        System.out.println(LocalDateTime.parse("2018-06-30T08:30:50"));

        // 自定义格式解析时间
        System.out.println(LocalDateTime.parse("2018-06-30 08:30:50",DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));

    }

    // 获取具体时间值
    @Test
    public void test_get(){

        LocalDateTime ldt = LocalDateTime.of(2018, 06, 30, 15, 30, 40, 100_000);
// LocalDateTime ldt = LocalDateTime.now();

        System.out.println("年:" + ldt.getYear());
        System.out.println("月:" + ldt.getMonthValue());
        System.out.println("日:" + ldt.getDayOfMonth());
        System.out.println("月份(英文):" + ldt.getMonth());
        System.out.println("星期(英文):" + ldt.getDayOfWeek());
        System.out.println("时:" + ldt.getHour());
        System.out.println("分:" + ldt.getMinute());
        System.out.println("秒:" + ldt.getSecond());
        System.out.println("毫秒:" + ldt.getNano());

        System.out.println("当月第几天:" + ldt.getDayOfMonth());
        System.out.println("当年第几天:" + ldt.getDayOfYear());

        // 通用get 获取方式
        System.out.println();
        System.out.println("年:" + ldt.get(ChronoField.YEAR));
        System.out.println("月:" + ldt.get(ChronoField.MONTH_OF_YEAR));
        System.out.println("日:" + ldt.get(ChronoField.DAY_OF_MONTH));
        System.out.println("本周第几天(数字):" + ldt.get(ChronoField.DAY_OF_WEEK));
        System.out.println("当年第几天:" + ldt.get(ChronoField.DAY_OF_YEAR));
        System.out.println("当月第几天%7:" + ldt.get(ChronoField.ALIGNED_DAY_OF_WEEK_IN_MONTH));
        System.out.println("当年第几天%7" + ldt.get(ChronoField.ALIGNED_DAY_OF_WEEK_IN_YEAR));
        System.out.println("当月第几周:" + ldt.get(ChronoField.ALIGNED_WEEK_OF_MONTH));
        System.out.println("当年第几周:" + ldt.get(ChronoField.ALIGNED_WEEK_OF_YEAR));
        System.out.println("据Unix 元年(1970.01.01)多少天:" + ldt.getLong(ChronoField.EPOCH_DAY));
        System.out.println();

        System.out.println("时:" + ldt.get(ChronoField.HOUR_OF_DAY));
        System.out.println("分:" + ldt.get(ChronoField.MINUTE_OF_HOUR));
        System.out.println("秒:" + ldt.get(ChronoField.SECOND_OF_MINUTE));
        System.out.println("毫秒:" + ldt.get(ChronoField.MILLI_OF_SECOND));
        System.out.println("微妙:" + ldt.getLong(ChronoField.MICRO_OF_SECOND));
        System.out.println("纳秒:" + ldt.get(ChronoField.NANO_OF_SECOND));
        System.out.println("当天第几分钟:" + ldt.get(ChronoField.MINUTE_OF_DAY));
        System.out.println("当天第几秒:" + ldt.get(ChronoField.SECOND_OF_DAY));
        System.out.println("当天第几毫秒:" + ldt.get(ChronoField.MILLI_OF_DAY));
        System.out.println("当天第几微妙:" + ldt.getLong(ChronoField.MICRO_OF_DAY));
        System.out.println("当天第几纳秒:" + ldt.getLong(ChronoField.NANO_OF_DAY));
        System.out.println("12小时制,取值范围[0,11],时:" + ldt.get(ChronoField.HOUR_OF_AMPM));
        System.out.println("12小时制,取值范围[1,12],时:" + ldt.get(ChronoField.CLOCK_HOUR_OF_AMPM));
        System.out.println("24小时制,取值范围[1,24],时:" + ldt.get(ChronoField.CLOCK_HOUR_OF_DAY));

    }

    // 时间计算
    @Test
    public void test_plus(){
        LocalDateTime ldt = LocalDateTime.of(2018, 06, 30, 15, 30, 40, 100_000);
// LocalDateTime ldt = LocalDateTime.now();

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

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

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

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

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

        System.out.println("3小时后:" + ldt.plusHours(3));
        System.out.println("20分钟后:" + ldt.plusMinutes(20));
        System.out.println("10秒后:" + ldt.plusSeconds(10));
        System.out.println("2_000_000纳秒后:" + ldt.plusNanos(2_000_000));
        System.out.println("3小时前:" + ldt.minusHours(3));
        System.out.println("20分钟前:" + ldt.minusMinutes(20));
        System.out.println("10秒钟前:" + ldt.minusSeconds(10));
        System.out.println("2_000_000秒钟前:" + ldt.minusNanos(2_000_000));
        System.out.println();

        System.out.println("3小时后:" + ldt.plus(Duration.ofHours(3)));
        System.out.println("20分钟后:" + ldt.plus(Duration.ofMinutes(20)));
        System.out.println("10秒钟后:" + ldt.plus(Duration.ofSeconds(10)));
        System.out.println("2000毫秒后:" + ldt.plus(Duration.ofMillis(200)));
        System.out.println("2_000_000纳秒后:" + ldt.plus(Duration.ofNanos(2_000_000)));
        System.out.println();

        System.out.println("3小时前:" + ldt.minus(Duration.ofHours(3)));
        System.out.println("20分钟前:" + ldt.minus(Duration.ofMinutes(20)));
        System.out.println("10秒钟前:" + ldt.minus(Duration.ofSeconds(10)));
        System.out.println("2000毫秒前:" + ldt.minus(Duration.ofMillis(200)));
        System.out.println("2_000_000纳秒前:" + ldt.minus(Duration.ofNanos(2_000_000)));
        System.out.println();

    }

    // 时间比较
    @Test
    public void test_compare(){

        LocalDateTime ldt0 = LocalDateTime.now().minusYears(3).minusHours(2);
        LocalDateTime ldt1 = LocalDateTime.now();
        LocalDateTime ldt2 = LocalDateTime.now().plusYears(3).plusHours(2);

        // 语义更直观
        System.out.println(ldt0.isBefore(ldt1));
        System.out.println(ldt0.isAfter(ldt2));

        // 正负数只能表示大小,0表示相等
        System.out.println(ldt0.compareTo(ldt2));
        System.out.println(ldt2.compareTo(ldt1));

    }

    // 修改时间
    @Test
    public void test_with(){

        LocalDateTime ldt = LocalDateTime.now();
        System.out.println("当期日期:" + ldt);
        System.out.println("替换年为2020:" + ldt.withYear(2020));
        System.out.println("替换月为5:" + ldt.withMonth(5));
        System.out.println("替换日为15:" + ldt.withDayOfMonth(15));
        System.out.println("替换为当年第100天:" + ldt.withDayOfYear(100));
        System.out.println("替换小时为15: " + ldt.withHour(15));
        System.out.println("替换分钟为35: " + ldt.withMinute(35));
        System.out.println("替换秒为55: " + ldt.withSecond(55));
        System.out.println("替换纳秒为300_000_000: " + ldt.withNano(300_000_000));
        System.out.println();

        System.out.println("获取当月第一天:" + ldt.with(TemporalAdjusters.firstDayOfMonth()));
        System.out.println("获取当年第一天:" + ldt.with(TemporalAdjusters.firstDayOfYear()));
        System.out.println("获取下月第一天:" + ldt.with(TemporalAdjusters.firstDayOfNextMonth()));
        System.out.println("获取来年第一天:" + ldt.with(TemporalAdjusters.firstDayOfNextYear()));
        System.out.println("获取当年最后一天:" + ldt.with(TemporalAdjusters.lastDayOfYear()));
        System.out.println("获取当月最后一天:" + ldt.with(TemporalAdjusters.lastDayOfMonth()));
        System.out.println("获取当月第一个周五:" + ldt.with(TemporalAdjusters.firstInMonth(DayOfWeek.FRIDAY)));
        System.out.println("获取当月最后一个周五:" + ldt.with(TemporalAdjusters.lastInMonth(DayOfWeek.FRIDAY)));
        System.out.println("获取当月下一个周五(不包括今天):" + ldt.with(TemporalAdjusters.next(DayOfWeek.FRIDAY)));
        System.out.println("获取当月下一个周五(包括今天):" + ldt.with(TemporalAdjusters.nextOrSame(DayOfWeek.FRIDAY)));
        System.out.println("获取当月上一个周五(不包括今天):" + ldt.with(TemporalAdjusters.previous(DayOfWeek.FRIDAY)));
        System.out.println("获取当月上一个周五(包括今天):" + ldt.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("获取下一个工作日:" + ldt.with(nextWorkingDay));
    }

    // 计算两个时间之间的差
    @Test
    public void test_until(){
        LocalDateTime ldt1 = LocalDateTime.of(2005, 9, 30, 18, 10, 20, 100_000);
        LocalDateTime ldt2 = LocalDateTime.of(2018, 3, 10, 8, 30, 40, 200_000);

        System.out.println("日期间隔-总年数:" + ldt1.until(ldt2, ChronoUnit.YEARS));
        System.out.println("日期间隔-总月数:" + ldt1.until(ldt2, ChronoUnit.MONTHS));
        System.out.println("日期间隔-总天数:" + ldt1.until(ldt2, ChronoUnit.DAYS));
        System.out.println("时间间隔-总小时数:" + ldt1.until(ldt2, ChronoUnit.HOURS));
        System.out.println("时间间隔-总分钟数:" + ldt1.until(ldt2, ChronoUnit.MINUTES));
        System.out.println("时间间隔-总秒数:" + ldt1.until(ldt2, ChronoUnit.SECONDS));
        System.out.println("时间间隔-总毫秒数:" + ldt1.until(ldt2, ChronoUnit.MILLIS));
        System.out.println("时间间隔-总微秒数:" + ldt1.until(ldt2, ChronoUnit.MICROS));
        System.out.println("时间间隔-总纳秒数:" + ldt1.until(ldt2, ChronoUnit.NANOS));
        System.out.println("时间间隔-总半天数(多少个12小时):" + ldt1.until(ldt2, ChronoUnit.HALF_DAYS));
        System.out.println("时间间隔-多少个十年:" + ldt1.until(ldt2, ChronoUnit.DECADES));
    }

    @Test
    public void test_between(){
        // LocalDateTime 计算日期间隔只能使用 Duration 或until 方法

        LocalDateTime ldt1 = LocalDateTime.of(2015, 9, 30, 18, 10, 20, 100_000);
        LocalDateTime ldt2 = LocalDateTime.of(2018, 3, 10, 8, 30, 40, 200_000);

        Duration duration = Duration.between(ldt1, ldt2);
        System.out.println(duration);
        System.out.println("时间间隔-总小时数:" + duration.toHours());
        System.out.println("时间间隔-总分钟数:" + duration.toMinutes());
        System.out.println("时间间隔-总秒数:" + duration.toMillis());
        System.out.println("时间间隔-总毫秒数:" + duration.toNanos());

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

    }

    @Test
    public void test_converter(){

        LocalDateTime ldt = LocalDateTime.now();
        System.out.println("当前时间日期:" + ldt);

        LocalDate ld = ldt.toLocalDate();
        System.out.println("当前日期:" + ld);

        LocalTime lt = ldt.toLocalTime();
        System.out.println("当前时间:" + lt);

        // 获取某个时区的时间
        ZonedDateTime zonedDateTime = ldt.atZone(ZoneId.of("US/Pacific"));
        System.out.println("时区时间:" + zonedDateTime); //2018-06-30T09:59:57.748-07:00[US/Pacific]

    }

}

相关文章