日期时间工具类
Posted Shinka_YXS
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了日期时间工具类相关的知识,希望对你有一定的参考价值。
package com.xxx.xxx.common.utils;
import com.xxx.xxx.common.enums.ResultCodeEnum;
import com.xxx.xxx.common.exception.InterceptorException;
import org.apache.commons.lang.StringUtils;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.Calendar;
import java.util.Date;
/**
* @description: 日期时间工具类
* @author: Shinka
* @date: 2021-08-24
*/
public final class DateUtil
private static DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
private static DateTimeFormatter dateTimeFormatterDeatil = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
private static SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
private static String rex = "\\\\d4-\\\\d2-\\\\d2";
/**
* Date 转 ZonedDateTime
* @param date
* @return
*/
public static ZonedDateTime date2ZoneDateTime(Date date)
return date == null ? null : ZonedDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());
/**
* String(yyyy-MM-dd) 转 Date
* @param StringDate
* @return
*/
public static Date string2Date(String StringDate)
if(StringUtils.isEmpty(StringDate))
throw new InterceptorException(ResultCodeEnum.STRING_IS_NULL.code(), ResultCodeEnum.STRING_IS_NULL.message());
if(!StringDate.matches(rex))
throw new InterceptorException(ResultCodeEnum.DATE_FORMAT_NOT_TRUE.code(), ResultCodeEnum.DATE_FORMAT_NOT_TRUE.message());
Date date = null;
try
date = simpleDateFormat.parse(StringDate);
catch (ParseException e)
e.printStackTrace();
return date;
/**
* String(yyyy-MM-dd) 转 ZonedDateTime
*
* @param StringDate 字符串日期
* @return ZonedDateTime
*/
public static ZonedDateTime string2ZonedDateTime(String StringDate)
if (StringUtils.isEmpty(StringDate))
return null;
if (!StringDate.matches(rex))
throw new InterceptorException(ResultCodeEnum.DATE_FORMAT_NOT_TRUE.code(), ResultCodeEnum.DATE_FORMAT_NOT_TRUE.message());
ZonedDateTime zonedDateTime = null;
try
zonedDateTime = LocalDate.parse(StringDate, dateTimeFormatter).atStartOfDay(ZoneId.systemDefault());
catch (Exception e)
e.printStackTrace();
return zonedDateTime;
/**
* string(yyyy-MM-dd) 转 ZonedDateTime+1天,主要用于日期至
* @param date yyyy-mm-dd
* @return zonedDateTime
*/
public static ZonedDateTime string2ZonedDateTimeAddOne(String date)
if (StringUtils.isEmpty(date))
return null;
Calendar c = Calendar.getInstance();
c.setTime(DateUtil.string2Date(date));
c.add(Calendar.DAY_OF_MONTH, 1);
Date time = c.getTime();
ZonedDateTime dateTo = DateUtil.date2ZoneDateTime(time);
return dateTo;
/**
* String(yyyy-MM-dd) 转 ZonedDateTime结束时间(系统时区)
* @param date
* @return
*/
public static ZonedDateTime getDayEndDate(String date)
return LocalDateTime.of(LocalDate.parse(date, dateTimeFormatter), LocalTime.MAX)
.atZone(ZoneId.systemDefault());
/**
* ZonedDateTime 转 String(yyyy-MM-dd)
* @param dateTime
* @return
*/
public static String zonedDateTimeToDateString(ZonedDateTime dateTime)
return zonedDateTimeToString(dateTime, dateTimeFormatter);
/**
* ZonedDateTime 转 String(yyyy-MM-dd HH:mm:ss)
* @param dateTime
* @return
*/
public static String zonedDateTimeToDateTimeString(ZonedDateTime dateTime)
return zonedDateTimeToString(dateTime, dateTimeFormatterDeatil);
/**
* ZonedDateTime 转 String(指定格式)
* @param dateTime
* @param formatter
* @return
*/
public static String zonedDateTimeToString(ZonedDateTime dateTime, DateTimeFormatter formatter)
if (dateTime == null)
return null;
String str = null;
try
str = dateTime.format(formatter);
catch (Exception e)
e.printStackTrace();
return str;
return str;
/**
* 今天的开始时间(系统时区)
* @return
*/
public static ZonedDateTime getCurrentDayStartDate()
return LocalDate.now().atStartOfDay(ZoneId.systemDefault());
/**
* 所传入ZonedDateTime的开始时间(默认为系统时区)
* @param zonedDateTime
* @return
*/
public static ZonedDateTime truncDate(ZonedDateTime zonedDateTime)
return null == zonedDateTime ? null : zonedDateTime.truncatedTo(ChronoUnit.DAYS);
/**
* 所传入ZonedDateTime的开始时间(UTC时间)
* @param zonedDateTime
* @return
*/
public static ZonedDateTime getStartDateByUTC(ZonedDateTime zonedDateTime)
return null == zonedDateTime ? null : zonedDateTime.toLocalDate().atStartOfDay(ZoneId.of("UTC"));
以上是关于日期时间工具类的主要内容,如果未能解决你的问题,请参考以下文章