/**
* ClassName SafDateUtil
* Saf Date operation Util
* A processing tool class for the local date time class in java1.8
*
* @author wangyu
* @version 1.0
* @date May 30, 2019 2:04:34 PM
* @since JDK 1.8
*/
public class SafDateUtil {
/**
* private constructor
*/
private SafDateUtil() {
}
/**
* get LocalTime
*
* @param receiveDate string date type
* @return LocalDateTime
*/
public static LocalDateTime getCurrentTimeFromReceiveDateTime(String receiveDate) {
return LocalDateTime.parse(receiveDate, DateTimeFormatter.ofPattern(DateFormatEnum.DATETIME.getType()));
}
/**
* Get T+1 Day From ReceiveDate
*
* @param receiveDate LocalData type Date
* @return java.time.LocalDate
*/
public static LocalDate getT1FromReceiveDate(LocalDate receiveDate) {
int dayOfMonth = receiveDate.getDayOfMonth() + 1;
int month = receiveDate.getMonthValue();
int year = receiveDate.getYear();
return LocalDate.of(year, month, dayOfMonth);
}
/**
* get T+1 LocalDateTime
*
* @param receiveDate LocalDateTime type date
* @return LocalDateTime
*/
public static LocalDateTime getLocalDateTimeT1(LocalDateTime receiveDate) {
int year = receiveDate.getYear();
int month = receiveDate.getMonthValue();
int dayOfMonth = receiveDate.getDayOfMonth() + 1;
int hour = receiveDate.getHour();
int minute = receiveDate.getMinute();
int second = receiveDate.getSecond();
int nano = receiveDate.getNano();
return LocalDateTime.of(year, month, dayOfMonth, hour, minute, second, nano);
}
/**
* Get LocalDate(yyyy-MM-dd) From localDateTime
*
* @param localDateTime LocalDateTime type
* @return LocalDate
*/
public static LocalDate localDateTimeFormatLocalDate(LocalDateTime localDateTime) {
return localDateTime.toLocalDate();
}
/**
* get LocalTime
*
* @param receiveDateTime string datetime
* @return java.time.LocalTime
* @author E091816
* @date 2019/5/16 14:56
*/
public static LocalTime getLocalTime(String receiveDateTime) {
LocalDateTime dateTime = LocalDateTime.parse(receiveDateTime, DateTimeFormatter.ofPattern(DateFormatEnum.DATETIME.getType()));
return dateTime.toLocalTime();
}
/**
* Get CurrentTime(HH:mm:ss) From receiveDate
*
* @param receiveDate string date
* @return java.time.LocalDate
* @author E091816
* @date 2019/5/17 11:49
*/
public static LocalDate getCurrentTimeFromReceiveDate(String receiveDate) {
return LocalDate.parse(receiveDate, DateTimeFormatter.ofPattern(DateFormatEnum.DATE.getType()));
}
/**
* Get Time(HH:mm:ss) From receiveDate
*
* @param receiveDate string date
* @return java.time.LocalTime
*/
public static LocalTime getDateTimeFromReceiveData(String receiveDate) {
DateTimeFormatter format = DateTimeFormatter.ofPattern(DateFormatEnum.TIME.getType());
return LocalTime.parse(receiveDate, format);
}
/**
* get T+1 LocalDate
*
* @param localDateTime LocalDateTime type
* @return LocalTime
*/
public static LocalTime getLocalTime(LocalDateTime localDateTime) {
return localDateTime.toLocalTime();
}
/**
* format the date to the string representation
*
* @param date the date to be formatted
* @param pattern date format to be used
* @return the date string formed based on pattern
*/
public static String format(Date date, String pattern) {
String returnValue = "";
if (date != null) {
SimpleDateFormat df = new SimpleDateFormat(pattern);
returnValue = df.format(date);
}
return returnValue;
}
/**
* parse string representation of date to Date object
*
* @param strDate string representation of a date
* @param pattern date format e.g. YYYY-MM-dd HH:mm:ss
* @return Date object if string representation is correspond to date format.
* otherwise, return null
*/
public static Date parse(String strDate, String pattern) {
SimpleDateFormat df = new SimpleDateFormat(pattern);
try {
return df.parse(strDate);
} catch (ParseException e) {
return null;
}
}
}
enum DateFormatEnum {
DATE("yyyy-MM-dd", "Date Format"),
TIME("HH:mm:ss", "Time Format"),
DATETIME("yyyy-MM-dd HH:mm:ss", "Date Time Format"),
DATETIME_WITH_MS("yyyy-MM-dd HH:mm:ss.SSS", "Date Time With Million Seconds Format");
private String type;
private String desc;
private DateFormatEnum(String type, String desc) {
this.type = type;
this.desc = desc;
}
public String getType() {
return this.type;
}
public String getDesc() {
return this.desc;
}
}