LocalDateTime使用詳解

爲啥Date遭嫌棄了

一、我想新建一個表示"此刻"的日期,打印出來:
在這裏插入圖片描述

java8中的 LocalDateTime

這玩意的出現就是爲了幹掉之前 JDK版本中的 Date老哥

代碼:

   package com.ruoyi.web.controller.platform;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.time.LocalDateTime;
import java.time.Month;
import java.time.format.DateTimeFormatter;

@RunWith(SpringRunner.class)
@SpringBootTest
public class LocalDateTimeUtils {
    private final static Logger logger = LoggerFactory.getLogger(LocalDateTimeUtils.class);

    @Test
    public void testLog() {
        logger.error("test -> error");
        logger.warn("test -> warn");
        logger.info("test -> info");
        logger.debug("test-> debug");
        //trace級別不會在控制檯打印輸出
        logger.trace("test -> trace");
        logger.info("這是何時==={}===", LocalDateTime.now());
    }

    @Test
    public void testLocalDateTime() {
        System.out.println("-----------------獲取當前此刻的時間----------------------------------");
        LocalDateTime localDateTime = LocalDateTime.now();
        logger.info("當前時刻:{}",localDateTime);
        logger.info("當前年份:{}",localDateTime.getYear());
        logger.info("當前月份:{}",localDateTime.getMonth());
        logger.info("當前日份:{}",localDateTime.getDayOfMonth());
        logger.info("當前時份:{}",localDateTime.getHour());
        logger.info("當前分份:{}",localDateTime.getMinute());
        logger.info("當前秒份:{}",localDateTime.getSecond());
        System.out.println("-----------------獲取當前此刻的時間----------------------------------");

        //比如,想構造:2019年10月12日9時21分32秒
        System.out.println("-----------------構造一個指定年、月、日的時間----------------------------------");
        LocalDateTime beforeDate = LocalDateTime.of(2019, Month.DECEMBER, 12, 9, 21, 32);
        logger.info("構造一個指定年、月、日的時間>>>>>>>>>{}>>>>>>>>>>",beforeDate);
        System.out.println("-----------------構造一個指定年、月、日的時間----------------------------------");

        //修改日期
        System.out.println("-----------------修改日期----------------------------------");
        LocalDateTime rightNow = LocalDateTime.now();
        rightNow = rightNow.minusYears(2);
        logger.info("減少2年==={}===", rightNow);
        rightNow = rightNow.plusMonths(3);
        logger.info("增加3個月==={}===", rightNow);
        rightNow = rightNow.withYear(2008);
        logger.info("直接修改年份到2008年==={}===", rightNow);
        rightNow = rightNow.withHour(13);
        logger.info("直接修改小時到13時==={}===", rightNow);
        System.out.println("-----------------修改日期----------------------------------");


        System.out.println("-----------------格式化日期 日期轉字符串----------------------------------");
        LocalDateTime dateTime = LocalDateTime.now();
        String format = dateTime.format(DateTimeFormatter.ISO_DATE);
        String format1 = dateTime.format(DateTimeFormatter.BASIC_ISO_DATE);
        String format2 = dateTime.format(DateTimeFormatter.ofPattern("yyyy/MM/dd"));
        String format3 = dateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        logger.info("格式化後的日期(基本樣式一舉例):>>>>{}>>>>", format);
        logger.info("格式化後的日期(基本樣式二舉例):>>>>{}>>>>", format1);
        logger.info("格式化後的日期(自定義樣式舉例):>>>>{}>>>>", format2);
        logger.info("格式化後的日期(自定義樣式舉例)日期時間轉成字符串:>>>>{}>>>>", format3);
        System.out.println("-----------------格式化日期 日期轉字符串----------------------------------");

        System.out.println("-----------------時間反解析 字符串轉日期----------------------------------");
        LocalDateTime time = LocalDateTime.parse("2020-03-13 14:41:14",DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        logger.info("字符串轉日期:>>>>{}>>>>",time);
        System.out.println("-----------------時間反解析 字符串轉日期----------------------------------");
    }
}

執行結果:

-----------------獲取當前此刻的時間----------------------------------
15:03:52.678 [main] INFO  c.r.w.c.p.LocalDateTimeUtils - [testLocalDateTime,34] - 當前時刻:2020-03-13T15:03:52.678
15:03:52.679 [main] INFO  c.r.w.c.p.LocalDateTimeUtils - [testLocalDateTime,35] - 當前年份:2020
15:03:52.679 [main] INFO  c.r.w.c.p.LocalDateTimeUtils - [testLocalDateTime,36] - 當前月份:MARCH
15:03:52.679 [main] INFO  c.r.w.c.p.LocalDateTimeUtils - [testLocalDateTime,37] - 當前日份:13
15:03:52.679 [main] INFO  c.r.w.c.p.LocalDateTimeUtils - [testLocalDateTime,38] - 當前時份:15
15:03:52.680 [main] INFO  c.r.w.c.p.LocalDateTimeUtils - [testLocalDateTime,39] - 當前分份:3
15:03:52.680 [main] INFO  c.r.w.c.p.LocalDateTimeUtils - [testLocalDateTime,40] - 當前秒份:52
-----------------獲取當前此刻的時間----------------------------------
-----------------構造一個指定年、月、日的時間----------------------------------
15:03:52.680 [main] INFO  c.r.w.c.p.LocalDateTimeUtils - [testLocalDateTime,46] - 構造一個指定年、月、日的時間>>>>>>>>>2019-12-12T09:21:32>>>>>>>>>>
-----------------構造一個指定年、月、日的時間----------------------------------
-----------------修改日期----------------------------------
15:03:52.680 [main] INFO  c.r.w.c.p.LocalDateTimeUtils - [testLocalDateTime,53] - 減少2年===2018-03-13T15:03:52.680===
15:03:52.680 [main] INFO  c.r.w.c.p.LocalDateTimeUtils - [testLocalDateTime,55] - 增加3個月===2018-06-13T15:03:52.680===
15:03:52.680 [main] INFO  c.r.w.c.p.LocalDateTimeUtils - [testLocalDateTime,57] - 直接修改年份到2008年===2008-06-13T15:03:52.680===
15:03:52.681 [main] INFO  c.r.w.c.p.LocalDateTimeUtils - [testLocalDateTime,59] - 直接修改小時到13時===2008-06-13T13:03:52.680===
-----------------修改日期----------------------------------
-----------------格式化日期 日期轉字符串----------------------------------
15:03:52.683 [main] INFO  c.r.w.c.p.LocalDateTimeUtils - [testLocalDateTime,69] - 格式化後的日期(基本樣式一舉例):>>>>2020-03-13>>>>
15:03:52.683 [main] INFO  c.r.w.c.p.LocalDateTimeUtils - [testLocalDateTime,70] - 格式化後的日期(基本樣式二舉例):>>>>20200313>>>>
15:03:52.683 [main] INFO  c.r.w.c.p.LocalDateTimeUtils - [testLocalDateTime,71] - 格式化後的日期(自定義樣式舉例):>>>>2020/03/13>>>>
15:03:52.683 [main] INFO  c.r.w.c.p.LocalDateTimeUtils - [testLocalDateTime,72] - 格式化後的日期(自定義樣式舉例)日期時間轉成字符串:>>>>2020-03-13 15:03:52>>>>
-----------------格式化日期 日期轉字符串----------------------------------
-----------------時間反解析 字符串轉日期----------------------------------
15:03:52.685 [main] INFO  c.r.w.c.p.LocalDateTimeUtils - [testLocalDateTime,77] - 字符串轉日期:>>>>2020-03-13T14:41:14>>>>
-----------------時間反解析 字符串轉日期----------------------------------
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章