import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.Month;
import java.time.Year;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;
import java.util.Date;
@Slf4j
public class TimeUtil {
private TimeUtil() {
}
public static int getNumOfYearFromLdt(LocalDateTime localDateTime) {
return Year.from(localDateTime).getValue();
}
public static int getNumOfMonthFromLdt(LocalDateTime localDateTime) {
return Month.from(localDateTime).getValue();
}
public static LocalDateTime getThePointedTime(Integer numOfYear,
Integer numOfMonth,
Integer numOfDay,
Integer numOfHour,
Integer numOfMinute,
Integer numOfSecond) {
return LocalDateTime.of(numOfYear, numOfMonth, numOfDay, numOfHour, numOfMinute, numOfSecond);
}
public static LocalDateTime getThePointedYear(Integer numOfYear) {
return getThePointedTime(numOfYear, 1, 1, 0, 0, 0);
}
public static LocalDateTime getThePointedMonth(Integer numOfMonth) {
int numOfYear = getNumOfYearFromLdt(LocalDateTime.now());
return getThePointedTime(numOfYear, numOfMonth, 1, 0, 0, 0);
}
public static LocalDateTime getThePointedDay(Integer numOfDay) {
int numOfYear = getNumOfYearFromLdt(LocalDateTime.now());
int numOfMonth = getNumOfMonthFromLdt(LocalDateTime.now());
return getThePointedTime(numOfYear, numOfMonth, numOfDay, 0, 0, 0);
}
public static LocalDateTime theLastDayTimeOfMonth(LocalDateTime localDateTime) {
return localDateTime.with(TemporalAdjusters.lastDayOfMonth()).withHour(23)
.withMinute(59)
.withSecond(59);
}
public static LocalDateTime theFirstDayTimeOfNextMonth(LocalDateTime localDateTime) {
return localDateTime.with(TemporalAdjusters.firstDayOfNextMonth())
.withHour(0)
.withMinute(0)
.withSecond(0);
}
public static LocalDateTime transferString2LocalDateTime(String dateTime, String patternValue) {
if (StringUtils.isBlank(dateTime) || StringUtils.isBlank(patternValue)) {
return null;
}
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(patternValue);
return LocalDateTime.parse(dateTime, formatter);
}
public static LocalDateTime transferDate2LocalDateTime(Date date) {
return date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
}
public static LocalDate transferString2LocalDate(String dateString, String patternValue) {
if (StringUtils.isBlank(dateString) || StringUtils.isBlank(patternValue)) {
return null;
}
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(patternValue);
return LocalDate.parse(dateString, formatter);
}
}
import lombok.Getter;
@Getter
public enum TimeEnumSummary {
YYYY_MM_DD("yyyy-MM-dd"),
YYYY_MM_DD_HH_MM("yyyy-MM-dd HH:mm"),
YYYY_MM_DD_HH_MM_SS("yyyy-MM-dd HH:mm:ss"),
;
private final String value;
TimeEnumSummary(String value) {
this.value = value;
}
}