Java中时间类转换Date、Calender 、LocalDateTime

时间:2024-10-14 18:22:15
package com.ptool.date; import lombok.AllArgsConstructor; import lombok.Getter; import java.text.SimpleDateFormat; import java.time.LocalDateTime; import java.time.ZoneId; import java.time.ZoneOffset; import java.time.ZonedDateTime; import java.time.format.DateTimeFormatter; import java.util.Calendar; import java.util.Date; import java.util.GregorianCalendar; /** * 时间工具类,主要用于三大时间类的关系转换 * @author PanXu */ public class DateUtils { /** * 时间格式和对应正则 */ @AllArgsConstructor enum TIME_FORMAT{ /** * 时间格式为强校验 */ STANDARD("yyyy-MM-dd HH:mm:ss", "^([1-2][0-9][0-9][0-9]-[0-1][0-9]-[0-3]{0,1}[0-9])\\s(20|21|22|23|[0-1]\\d):[0-5]\\d:[0-5]\\d"); /** * 时间格式 */ @Getter private final String value; /** * 正则 */ @Getter private final String regex; /** * 标准时间格式校验 * @param timeStr * @return */ public Boolean checkStandardTimeStr(String timeStr){ if ( timeStr == null || timeStr.length() < 1){ return false; } if( timeStr.matches(STANDARD.getRegex())){ return true; } return false; } } /** * date 转 标准时间字符 * @param date * @return */ public static String dateToStandardString(Date date){ if( date == null){ return null; } return new SimpleDateFormat(TIME_FORMAT.STANDARD.getValue()).format(date); } /** * Calendar 转 标准时间字符 * @param calendar * @return */ public static String calendarToStandardString(Calendar calendar){ if( calendar == null){ return null; } return new SimpleDateFormat(TIME_FORMAT.STANDARD.getValue()).format(calendar.getTimeInMillis()); } /** * localDateTime 转 标准时间字符 * @param localDateTime * @return */ public static String localDateTimeToStandardString(LocalDateTime localDateTime){ if( localDateTime == null ){ return null; } return localDateTime.format(DateTimeFormatter.ofPattern(TIME_FORMAT.STANDARD.getValue())); } /** * 标准时间字符 转 Date * @param timeStr * @return */ public static Date standardTimeStrToDate(String timeStr){ if( timeStr == null){ return null; } if( TIME_FORMAT.STANDARD.checkStandardTimeStr(timeStr)){ try { return new SimpleDateFormat(TIME_FORMAT.STANDARD.getValue()).parse(timeStr); }catch (Exception e){ // 格式强校验 e.printStackTrace(); } } return null; } /** * 标准时间字符 转 Calendar * @param timeStr * @return */ public static Calendar standardTimeStrToCalendar(String timeStr){ if( timeStr == null){ return null; } if( TIME_FORMAT.STANDARD.checkStandardTimeStr(timeStr)){ try { Calendar calendar = Calendar.getInstance(); Date date = new SimpleDateFormat(TIME_FORMAT.STANDARD.getValue()).parse(timeStr); calendar.setTime(date); return calendar; }catch (Exception e){ // 格式强校验 e.printStackTrace(); } } return null; } /** * 标准时间字符 转 localDateTime * @param timeStr * @return */ public static LocalDateTime standardTimeStrToLocalDateTime(String timeStr){ if( timeStr == null){ return null; } if( TIME_FORMAT.STANDARD.checkStandardTimeStr(timeStr)){ return LocalDateTime.parse(timeStr,DateTimeFormatter.ofPattern(TIME_FORMAT.STANDARD.getValue())); } return null; } /** * 时间对象转标准输出 * @param obj * @return */ public static String objectToStandardTimeStr(Object obj){ if( obj == null){ return null; } if( obj instanceof Date){ return dateToStandardString((Date) obj); } if( obj instanceof Calendar){ return calendarToStandardString((Calendar) obj); } if( obj instanceof LocalDateTime){ return localDateTimeToStandardString((LocalDateTime) obj); } return null; } /** * date 转 LocalDateTime * @param date * @return */ public static LocalDateTime dateToLocalDateTime(Date date) { if( date == null ){ return null; } return date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime(); } /** * LocalDateTime 转 date * @param localDateTime * @return */ public static Date localDateTimeToDate(LocalDateTime localDateTime) { if (localDateTime == null) { return null; } return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant()); } /** * Calendar 转 date * @param calendar * @return */ public static Date calenderToDate(Calendar calendar){ if (calendar == null) { return null; } return new Date(calendar.getTimeInMillis()); } /** * date 转 Calendar * @param date * @return */ public static Calendar dateToCalendar(Date date){ if (date == null) { return null; } Calendar calendar = Calendar.getInstance(); calendar.setTime(date); return calendar; } /** * Calendar 转 LocalDateTime * @param calendar * @return */ public static LocalDateTime calenderToLocalDateTime(Calendar calendar){ if (calendar == null) { return null; } return LocalDateTime.ofInstant(calendar.toInstant(), ZoneOffset.systemDefault()); } /** * LocalDateTime 转 Calendar * @param localDateTime * @return */ public static Calendar localDateTimeToCalender(LocalDateTime localDateTime){ if (localDateTime == null) { return null; } return GregorianCalendar.from(ZonedDateTime.of(localDateTime, ZoneId.systemDefault())); } /** * 获得当天的起始时间 * @return */ public static String getTodayStartStr(){ return getTimeStartOrEndStr(new Date(),1); } /** * 获得当天的结束时间 * @return */ public static String getTodayEndStr(){ return getTimeStartOrEndStr(new Date(),2); } /** * * @param date * @param type 1-开始时间 2-结束时间 * @return */ public static String getTimeStartOrEndStr(Date date, Integer type){ // 开始时间 if( Integer.valueOf(1).equals(type)){ Calendar calendar = dateToCalendar(date); calendar.set(Calendar.SECOND, 0); calendar.set(Calendar.MINUTE, 0); calendar.set(Calendar.HOUR_OF_DAY, 0); calendar.set(Calendar.MILLISECOND, 0); return calendarToStandardString(calendar); } // 结束时间 if( Integer.valueOf(2).equals(type)){ Calendar calendar = dateToCalendar(date); calendar.set(Calendar.SECOND, 59); calendar.set(Calendar.MINUTE, 59); calendar.set(Calendar.HOUR_OF_DAY, 23); calendar.set(Calendar.MILLISECOND, 0); return calendarToStandardString(calendar); } return null; } }