web123456

Java time class conversion Date, Calender, LocalDateTime

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; /** * Time tool class, mainly used for the three major time class relationship conversion * @author PanXu */ public class DateUtils { /** * :: Time format and corresponding regularity */ @AllArgsConstructor enum TIME_FORMAT{ /** * Time format is strongly calibrated */ 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"); /** * :: Time format */ @Getter private final String value; /** * Regular */ @Getter private final String regex; /** * Standard time format checksum * @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 to standard time characters * @param date * @return */ public static String dateToStandardString(Date date){ if( date == null){ return null; } return new SimpleDateFormat(TIME_FORMAT.STANDARD.getValue()).format(date); } /** * Calendar to standard time characters * @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 to standard time character. * @param localDateTime * @return */ public static String localDateTimeToStandardString(LocalDateTime localDateTime){ if( localDateTime == null ){ return null; } return localDateTime.format(DateTimeFormatter.ofPattern(TIME_FORMAT.STANDARD.getValue())); } /** * Standard time character to 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){ // Strong format checking e.printStackTrace(); } } return null; } /** * Standard time character to 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){ // Strong format checking e.printStackTrace(); } } return null; } /** * Standard time character to 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; } /** * Time object to standard output * @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 to LocalDateTime * @param date * @return */ public static LocalDateTime dateToLocalDateTime(Date date) { if( date == null ){ return null; } return date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime(); } /** * LocalDateTime to date * @param localDateTime * @return */ public static Date localDateTimeToDate(LocalDateTime localDateTime) { if (localDateTime == null) { return null; } return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant()); } /** * Calendar to date * @param calendar * @return */ public static Date calenderToDate(Calendar calendar){ if (calendar == null) { return null; } return new Date(calendar.getTimeInMillis()); } /** * date to 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 to LocalDateTime * @param calendar * @return */ public static LocalDateTime calenderToLocalDateTime(Calendar calendar){ if (calendar == null) { return null; } return LocalDateTime.ofInstant(calendar.toInstant(), ZoneOffset.systemDefault()); } /** * LocalDateTime to Calendar * @param localDateTime * @return */ public static Calendar localDateTimeToCalender(LocalDateTime localDateTime){ if (localDateTime == null) { return null; } return GregorianCalendar.from(ZonedDateTime.of(localDateTime, ZoneId.systemDefault())); } /** * Get the start time of the day * @return */ public static String getTodayStartStr(){ return getTimeStartOrEndStr(new Date(),1); } /** * Get the end of the day * @return */ public static String getTodayEndStr(){ return getTimeStartOrEndStr(new Date(),2); } /** /** /** * @param date * @param type 1-start time 2-end time * @return */ public static String getTimeStartOrEndStr(Date date, Integer type){ // Start time 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); } // End time 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; } }