为啥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>>>>
-----------------时间反解析 字符串转日期----------------------------------
@Test
public void test1() {
String nowDateStr = LocalDate.now().toString();
logger.info("当前日期:{}", nowDateStr);
LocalDate nowDate = LocalDate.parse("2018-03-25");
logger.info("格式化指定日期,字符串转日期:{}", nowDate.toString());
String nowTimeStr = LocalTime.now().toString();
System.out.println(nowTimeStr);
logger.info("获取当前时间:{}", nowTimeStr);
LocalTime nowTime = LocalTime.parse("12:10:13");
logger.info("格式化指定时间,字符串转时间:{}", nowTime.toString());
logger.info("====获取当前时间:{}", LocalDateTime.now().toString());
logger.info("====获取日期:{}", LocalDateTime.now().toLocalDate().toString());
logger.info("====获取时间:{}", LocalDateTime.now().toLocalTime().toString());
logger.info("====获取最大值:{}", LocalDateTime.MAX.toString());
logger.info("====获取最小值:{}", LocalDateTime.MIN.toString());
}
/**
* Date转换为LocalDateTime
*
* @param date
*/
public void date2LocalDateTime(Date date) {
//An instantaneous point on the time-line.(时间线上的一个瞬时点。)
Instant instant = date.toInstant();
//A time-zone ID, such as {@code Europe/Paris}.(时区)
ZoneId zoneId = ZoneId.systemDefault();
LocalDateTime localDateTime = instant.atZone(zoneId).toLocalDateTime();
//2018-03-27T14:07:32.668
System.out.println(localDateTime.toString());
//2018-03-27 14:48:57.453
System.out.println(localDateTime.toLocalDate() + " " + localDateTime.toLocalTime());
//This class is immutable and thread-safe.@since 1.8
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
//2018-03-27 14:52:57
System.out.println(dateTimeFormatter.format(localDateTime));
}
/**
* LocalDateTime转换为Date
*
* @param localDateTime
*/
public void localDateTime2Date(LocalDateTime localDateTime) {
ZoneId zoneId = ZoneId.systemDefault();
//Combines this date-time with a time-zone to create a ZonedDateTime.
ZonedDateTime zdt = localDateTime.atZone(zoneId);
Date date = Date.from(zdt.toInstant());
//Tue Mar 27 14:17:17 CST 2018
System.out.println(date.toString());
}