Java日期工具類操作字符串Date和LocalDate互轉
更新時間:2022年06月15日 08:59:07 作者:? 共飲一杯無? ?
這篇文章主要介紹了Java日期工具類操作字符串Date和LocalDate互轉,文章首先通過需要先引入坐標展開主題的相關內(nèi)容介紹,需要的朋友可以參一下
前言:
避免重復造輪子,相關方法基于hutool日期時間工具封裝并做部分增強。需要先引入如下坐標
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.4.7</version>
</dependency> 字符串轉Date
//字符串轉Date
Date dateTime = DateUtil.parseDate("2022-04-06");
System.out.println(dateTime);Date轉字符串
//Date轉字符串不指定format格式,默認yyyy-MM-dd HH:mm:ss
String dateStr = DateUtil.date2Str(new Date());
System.out.println(dateStr);
//Date轉字符串指定格式
String dateStr2 = DateUtil.date2Str("yyyy/MM/dd",new Date());
System.out.println(dateStr2);字符串轉LocalDate
//字符串轉LocalDate
LocalDate localDate = DateUtil.parseLocalDate("2022-04-06");
System.out.println(localDate);Date轉LocalDate
//Date轉LocalDate LocalDate localDate = DateUtil.date2LocalDate(new Date()); System.out.println(localDate);
LocalDate轉字符串
//LocalDate轉Str String localDateStr = DateUtil.localDate2Str(LocalDate.now()); System.out.println(localDateStr);
兩個日期的時間差
String beginDateStr = "2022-02-01 22:33:23"; Date beginDate = DateUtil.parse(beginDateStr); String endDateStr = "2022-03-10 23:33:23"; Date endDate = DateUtil.parse(endDateStr); //相差天數(shù)(37) long betweenDay = DateUtil.between(beginDate, endDate, DateUnit.DAY); System.out.println(betweenDay); //格式化時間差(37天1小時) String formatBetween = DateUtil.formatBetween(beginDate, endDate, BetweenFormater.Level.HOUR); System.out.println(formatBetween);
一天的開始和結束時間
String dateStr = "2022-04-07 10:33:23"; Date date = DateUtil.parse(dateStr); //一天的開始時間:2022-04-07 00:00:00 Date beginOfDay = DateUtil.beginOfDay(date); System.out.println(beginOfDay); //一天的結束時間:2022-04-07 23:59:59 Date endOfDay = DateUtil.endOfDay(date); System.out.println(endOfDay);
工具類
/**
* <p>基于hutool的日期工具類增強</p>
*
* @author zjq
* @date 2021/04/07
*/
public class DateUtil extends cn.hutool.core.date.DateUtil {
private static final String[] PARSE_PATTERNS = {
"yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy-MM",
"yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm", "yyyy/MM",
"yyyy.MM.dd", "yyyy.MM.dd HH:mm:ss", "yyyy.MM.dd HH:mm", "yyyy.MM"};
public static final DateTimeFormatter TIME_FORMATTER = DateTimeFormatter.ofPattern("HH:mm:ss");
public static final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd");
public static final DateTimeFormatter DATETIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
/**
* 字符串轉date類型
*
* @param dateStr
* @return
*/
public static Date parseDate(Object dateStr) {
if (ObjectUtils.isNull(dateStr)) {
return null;
}
if (dateStr instanceof Double) {
return org.apache.poi.ss.usermodel.DateUtil.getJavaDate((Double) dateStr);
}
return parse(dateStr.toString(), PARSE_PATTERNS);
}
/**
* Date類型轉字符串
*
* @param date
* @return yyyy-MM-dd HH:mm:ss
*/
public static String date2Str(Date date) {
return date2Str(null, date);
}
/**
* Date類型轉字符串
*
* @param format
* @param date
* @return
*/
public static String date2Str(String format, Date date) {
if (ObjectUtils.isNull(date)) {
return null;
}
SimpleDateFormat dateFormat = StrUtil.isNotBlank(format) ?new SimpleDateFormat(format):
new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return dateFormat.format(date);
}
/**
* 字符串轉LocalTime類型
*
* @param dateStr
* @return
*/
public static LocalTime parseLocalTime(Object dateStr) {
if (ObjectUtils.isNull(dateStr)) {
return null;
}
if (dateStr instanceof Double) {
return date2LocalTime(parseDate(dateStr));
}
return LocalTime.parse(dateStr.toString(), TIME_FORMATTER);
}
/**
* Date轉LocalTime
*
* @param date
* @return
*/
public static LocalTime date2LocalTime(Date date) {
if (null == date) {
return null;
}
return date.toInstant().atZone(ZoneId.systemDefault()).toLocalTime();
}
/**
* LocalTime轉Str
*
* @param localTime
* @return HH:mm:ss
*/
public static String localTime2Str(LocalTime localTime) {
return localTime2Str(null, localTime);
}
/**
* LocalTime轉str
*
* @param format 格式
* @param localTime
* @return
*/
public static String localTime2Str(String format, LocalTime localTime) {
if (null == localTime) {
return null;
}
DateTimeFormatter formatter = StrUtil.isBlank(format) ?
TIME_FORMATTER : DateTimeFormatter.ofPattern(format);
return localTime.format(formatter);
}
/**
* 字符串轉LocalDate類型
*
* @param dateStr
* @return
*/
public static LocalDate parseLocalDate(Object dateStr) {
if (ObjectUtils.isNull(dateStr)) {
return null;
}
if (dateStr instanceof Double) {
return date2LocalDate(parseDate(dateStr));
}
return LocalDate.parse(dateStr.toString(), DATE_FORMATTER);
}
/**
* Date轉LocalDate
*
* @param date
* @return
*/
public static LocalDate date2LocalDate(Date date) {
if (null == date) {
return null;
}
return date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
}
/**
* LocalDate轉Str
*
* @param localDate
* @return
*/
public static String localDate2Str(LocalDate localDate) {
return localDate2Str(null, localDate);
}
/**
* LocalDate轉Str
*
* @param format 格式
* @param localDate
* @return
*/
public static String localDate2Str(String format, LocalDate localDate) {
if (null == localDate) {
return null;
}
DateTimeFormatter formatter = StrUtil.isBlank(format) ?
DATE_FORMATTER : DateTimeFormatter.ofPattern(format);
return localDate.format(formatter);
}
/**
* 字符串轉LocalDateTime類型
*
* @param dateStr
* @return
*/
public static LocalDateTime parseLocalDateTime(Object dateStr) {
if (ObjectUtils.isNull(dateStr)) {
return null;
}
if (dateStr instanceof Double) {
return date2LocalDateTime(parseDate(dateStr));
}
return LocalDateTime.parse(dateStr.toString(), DATETIME_FORMATTER);
}
/**
* Date轉LocalDateTime
*
* @param date
* @return
*/
public static LocalDateTime date2LocalDateTime(Date date) {
if (null == date) {
return null;
}
return date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
}
/**
* LocalDate轉Str
*
* @param localDateTime
* @return
*/
public static String localDateTime2Str(LocalDateTime localDateTime) {
return localDateTime2Str(null, localDateTime);
}
/**
* LocalDate轉Str
*
* @param format
* @param localDateTime
* @return
*/
public static String localDateTime2Str(String format, LocalDateTime localDateTime) {
if (null == localDateTime) {
return null;
}
DateTimeFormatter formatter = StrUtil.isBlank(format) ?
DATETIME_FORMATTER : DateTimeFormatter.ofPattern(format);
return localDateTime.format(formatter);
}
}到此這篇關于Java日期工具操作字符串Date和LocalDate互轉的文章就介紹到這了,更多相關Date和LocalDate互轉內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
springBoot解決static和@Component遇到的bug
這篇文章主要介紹了springBoot解決static和@Component遇到的bug,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-02-02
log4j2采用AsyncLogger出現(xiàn)的錯誤及解決方案
這篇文章主要介紹了log4j2采用AsyncLogger出現(xiàn)的錯誤及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-12-12
SpringBoot如何使用p6spy監(jiān)控數(shù)據(jù)庫
這篇文章主要介紹了SpringBoot如何使用p6spy監(jiān)控數(shù)據(jù)庫問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-01-01
詳解json string轉換為java bean及實例代碼
這篇文章主要介紹了詳解json string轉換為java bean及實例代碼的相關資料,這里提供實例代碼幫助大家理解,需要的朋友可以參考下2017-07-07
Java實現(xiàn)利用廣度優(yōu)先遍歷(BFS)計算最短路徑的方法
這篇文章主要介紹了Java實現(xiàn)利用廣度優(yōu)先遍歷(BFS)計算最短路徑的方法,實例分析了廣度優(yōu)先遍歷算法的原理與使用技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-04-04

