mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-04-19 03:01:48 +08:00
!97 关于jdk8新时间类型LocalDateTime的相关方法
Merge pull request !97 from 27号/v5-dev
This commit is contained in:
commit
154786ebe0
@ -468,10 +468,10 @@ public class Convert {
|
||||
* @return 结果
|
||||
* @since 5.0.7
|
||||
*/
|
||||
public static Date toLocalDateTime(Object value, Date defaultValue) {
|
||||
public static LocalDateTime toLocalDateTime(Object value, LocalDateTime defaultValue) {
|
||||
return convertQuietly(LocalDateTime.class, value, defaultValue);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Instant<br>
|
||||
* 如果给定的值为空,或者转换失败,返回默认值<br>
|
||||
@ -498,7 +498,19 @@ public class Convert {
|
||||
public static Date toDate(Object value) {
|
||||
return toDate(value, null);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 转换为LocalDateTime<br>
|
||||
* 如果给定的值为空,或者转换失败,返回<code>null</code><br>
|
||||
* 转换失败不会报错
|
||||
*
|
||||
* @param value 被转换的值
|
||||
* @return 结果
|
||||
*/
|
||||
public static LocalDateTime toLocalDateTime(Object value) {
|
||||
return toLocalDateTime(value, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* 转换为Enum对象<br>
|
||||
* 如果给定的值为空,或者转换失败,返回默认值<br>
|
||||
|
@ -513,7 +513,32 @@ public class DateUtil {
|
||||
}
|
||||
|
||||
// ------------------------------------ Format start ----------------------------------------------
|
||||
|
||||
|
||||
/**
|
||||
* 格式化日期时间<br>
|
||||
* 格式 yyyy-MM-dd HH:mm:ss
|
||||
*
|
||||
* @param localDateTime 被格式化的日期
|
||||
* @return 格式化后的字符串
|
||||
*/
|
||||
public static String formatLocalDateTime(LocalDateTime localDateTime) {
|
||||
return format(localDateTime, DatePattern.NORM_DATETIME_PATTERN);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据特定格式格式化日期
|
||||
* @param localDateTime 被格式化的日期
|
||||
* @param format 日期格式,常用格式见: {@link DatePattern}
|
||||
* @return 格式化后的字符串
|
||||
*/
|
||||
public static String format(LocalDateTime localDateTime, String format) {
|
||||
if (null == localDateTime || StrUtil.isBlank(format)) {
|
||||
return null;
|
||||
}
|
||||
DateTimeFormatter df = DateTimeFormatter.ofPattern(format);
|
||||
return localDateTime.format(df);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据特定格式格式化日期
|
||||
*
|
||||
@ -668,7 +693,31 @@ public class DateUtil {
|
||||
// ------------------------------------ Format end ----------------------------------------------
|
||||
|
||||
// ------------------------------------ Parse start ----------------------------------------------
|
||||
|
||||
|
||||
/**
|
||||
* 构建LocalDateTime对象<br/>
|
||||
* 格式:yyyy-MM-dd HH:mm:ss
|
||||
*
|
||||
* @param dateStr 时间字符串(带格式)
|
||||
* @return LocalDateTime对象
|
||||
*/
|
||||
public static LocalDateTime parseLocalDateTime(CharSequence dateStr) {
|
||||
return parseLocalDateTime(dateStr, DatePattern.NORM_DATETIME_PATTERN);
|
||||
}
|
||||
|
||||
/**
|
||||
* 构建LocalDateTime对象
|
||||
* @param dateStr 时间字符串(带格式)
|
||||
* @param format 使用{@link DatePattern}定义的格式
|
||||
* @return LocalDateTime对象
|
||||
*/
|
||||
public static LocalDateTime parseLocalDateTime(CharSequence dateStr, String format) {
|
||||
dateStr = normalize(dateStr);
|
||||
DateTimeFormatter df = DateTimeFormatter.ofPattern(format);
|
||||
LocalDateTime ldt = LocalDateTime.parse(dateStr,df);
|
||||
return ldt;
|
||||
}
|
||||
|
||||
/**
|
||||
* 构建DateTime对象
|
||||
*
|
||||
|
@ -1,13 +1,15 @@
|
||||
package cn.hutool.core.convert;
|
||||
|
||||
import cn.hutool.core.date.DateUtil;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.sql.Timestamp;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.Date;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import cn.hutool.core.date.DateUtil;
|
||||
|
||||
public class DateConvertTest {
|
||||
|
||||
@Test
|
||||
@ -51,4 +53,20 @@ public class DateConvertTest {
|
||||
java.sql.Date value2 = Convert.convert(java.sql.Date.class, timeLong);
|
||||
Assert.assertEquals(timeLong, value2.getTime());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void toLocalDateTimeTest() {
|
||||
Date src = new Date();
|
||||
|
||||
LocalDateTime ldt = Convert.toLocalDateTime(src);
|
||||
Assert.assertEquals(ldt, DateUtil.toLocalDateTime(src));
|
||||
|
||||
Timestamp ts = Timestamp.from(src.toInstant());
|
||||
ldt = Convert.toLocalDateTime(ts);
|
||||
Assert.assertEquals(ldt, DateUtil.toLocalDateTime(src));
|
||||
|
||||
String str = "2020-12-12 12:12:12.0";
|
||||
ldt = Convert.toLocalDateTime(str);
|
||||
Assert.assertEquals(ldt.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.S")), str);
|
||||
}
|
||||
}
|
||||
|
@ -658,4 +658,20 @@ public class DateUtilTest {
|
||||
boolean expired = DateUtil.isExpired(startDate, DateField.DAY_OF_YEAR, length, endDate);
|
||||
Assert.assertTrue(expired);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void localDateTimeTest() {
|
||||
// 测试字符串与LocalDateTime的互相转换
|
||||
String strDate = "2019-12-01 17:02:30";
|
||||
LocalDateTime ldt = DateUtil.parseLocalDateTime(strDate);
|
||||
String strDate1 = DateUtil.formatLocalDateTime(ldt);
|
||||
System.out.println(strDate1);
|
||||
Assert.assertEquals(strDate, strDate1);
|
||||
|
||||
strDate = "2019年12月01日 17:02:30.111";
|
||||
ldt = DateUtil.parseLocalDateTime(strDate, DatePattern.NORM_DATETIME_MS_PATTERN);
|
||||
strDate1 = DateUtil.format(ldt, DatePattern.NORM_DATETIME_MS_PATTERN);
|
||||
System.out.println(strDate1);
|
||||
Assert.assertEquals(strDate, strDate1);
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user