JDK8 Date Time API

废话不说上代码。

  @Test
    public void test01() {
        // 获取本地时间
        LocalDateTime currentDate = LocalDateTime.now();
        System.out.println("当前时间:" + currentDate);

        // 获取当前日期的日期
        LocalDate date = currentDate.toLocalDate();
        System.out.println("日期:  " + date);
        // 获取当前日期的时间
        LocalTime time = currentDate.toLocalTime();
        System.out.println("时间: " + time);

        int year = currentDate.getYear();
        Month month = currentDate.getMonth();
        // 获取新的一年到今天共多少天
        int dayOfYear = currentDate.getDayOfYear();
        System.out.println("dayOfYear = " + dayOfYear);
        // 获取本月第几日
        int dayOfMonth = currentDate.getDayOfMonth();
        System.out.println("dayOfMonth = " + dayOfMonth);
        // 获取后去本周是周几
        DayOfWeek dayOfWeek = currentDate.getDayOfWeek();
        System.out.println("dayOfWeek = " + dayOfWeek);
        // 修改 年份和月份
        LocalDateTime dateTime = currentDate.withYear(2020).withDayOfMonth(16);
        System.out.println("dateTime = " + dateTime);
        // 自定义日期
        LocalDate ofDate = LocalDate.of(2021, Month.JUNE, 16);
        System.out.println("ofDate = " + ofDate);
        // 自定义时间
        LocalTime ofTime = LocalTime.of(12, 12);
        System.out.println("ofTime = " + ofTime);
        System.out.println("年:" + year + "  月:" + month + "  天:" + dayOfYear);
    }
==================================================================================
/**
 * 时间比较
 */
@Test
public void diffTest() {
    LocalDateTime now = LocalDateTime.now();
    LocalDateTime yestory = now.minusDays(1);
    System.out.println(now + "在" + yestory + "之后吗?" + now.isAfter(yestory));
    System.out.println(now + "在" + yestory + "之前吗?" + now.isBefore(yestory));
 
    // 时间差
    long day = yestory.until(now, ChronoUnit.DAYS);
    long month = yestory.until(now, ChronoUnit.MONTHS);
    long hours = yestory.until(now, ChronoUnit.HOURS);
    long minutes = yestory.until(now, ChronoUnit.MINUTES);
    System.out.println("相差月份" + month);
    System.out.println("相差天数" + day);
    System.out.println("相差小时" + hours);
    System.out.println("相差分钟" + minutes);
}
===============================================================================================
/**
 * 时间方法
 */
@Test
public void timeFunctionTest() {
    LocalDateTime now = LocalDateTime.now();
    System.out.println("当前时间:" + now);
    // 第一天
    LocalDateTime firstDay = now.withDayOfMonth(1);
    System.out.println("本月第一天:" + firstDay);
    // 当天最后一秒
    LocalDateTime lastSecondOfDay = now.withHour(23).withMinute(59).withSecond(59);
    System.out.println("当天最后一秒:" + lastSecondOfDay);
    // 最后一天
    LocalDateTime lastDay = now.with(TemporalAdjusters.lastDayOfMonth());
    System.out.println("本月最后一天:" + lastDay);
    // 是否闰年
    System.out.println("今年是否闰年:" + Year.isLeap(now.getYear()));
}
===============================================================================================
/**
 * 日期格式化
 */
@Test
public void formatTest() {
    LocalDateTime now = LocalDateTime.now();
    System.out.println("当前时间:" + now);
    System.out.println("格式化后:" + now.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME));
    System.out.println("格式化后:" + now.format(DateTimeFormatter.ISO_LOCAL_DATE));
    System.out.println("格式化后:" + now.format(DateTimeFormatter.ISO_LOCAL_TIME));
    System.out.println("格式化后:" + now.format(DateTimeFormatter.ofPattern("YYYY-MM-dd hh:mm:ss")));
}

输出结果:

// 基本用法
当前时间:2021-03-30T14:24:42.643
日期:  2021-03-30
时间: 14:24:42.643
dayOfYear = 89
dayOfMonth = 30
dayOfWeek = TUESDAY
dateTime = 2020-03-16T14:24:42.643
ofDate = 2021-06-16
ofTime = 12:12
年:2021  月:MARCH  天:89
===============================================================================================
// 时间比较
2019-10-24T00:39:01.5892019-10-23T00:39:01.589之后吗?true
2019-10-24T00:39:01.5892019-10-23T00:39:01.589之前吗?false
相差月份0
相差天数1
相差小时24
相差分钟1440
输出结果:
===============================================================================================
// 时间的用法
当前时间:2019-10-24T00:43:28.296
本月第一天:2019-10-01T00:43:28.296
当天最后一秒:2019-10-24T23:59:59.296
本月最后一天:2019-10-31T00:43:28.296
今年是否闰年:false
===============================================================================================
//日期格式化
当前时间:2019-10-24T00:37:44.867
格式化后:2019-10-24T00:37:44.867
格式化后:2019-10-24
格式化后:00:37:44.867
格式化后:2019-10-24 12:37:44

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值