import java.time.*;
import java.time.format.DateTimeFormatter;
public class LocalDateTimeUtil {
private static final String YYYY_MM_DD = “yyyy-MM-dd”;
public static String format(LocalDateTime datetime, DateTimeFormatter formatter) {
return formatter.format(datetime);
}
/**
* 对LocalDateTime进行指定格式的格式换并转日期字符串
*
* @param datetime LocalDateTime
* @param pattern 格式 如:yyyy-MM-dd
* @return 日期字符串
*/
public static String format(LocalDateTime datetime, String pattern) {
return DateTimeFormatter.ofPattern(pattern).format(datetime);
}
/**
* 10位int时间转为LocalDateTime
*
* @param timeInt 10位时间秒
* @return LocalDateTime
*/
public static LocalDateTime getLocalDateTime(int timeInt) {
Instant instant = Instant.ofEpochSecond(timeInt);
return LocalDateTime.ofInstant(instant, ZoneId.of("Asia/Shanghai"));
}
/**
* 10位int时间转为LocalDateTime
*
* @param timeInt 10位时间秒
* @return LocalDateTime
*/
public static LocalDate getLocalDate(int timeInt) {
Instant instant = Instant.ofEpochSecond(timeInt);
return LocalDate.ofInstant(instant, ZoneId.of("Asia/Shanghai"));
}
/**
* 获取当前时间秒
*
* @return 时间秒(10位)
*/
public static int getNowSeconds() {
return toIntSeconds(LocalDateTime.now());
}
/**
* LocalDateTime转为时间秒
*
* @param datetime LocalDateTime
* @return 时间秒(10位)
*/
public static int toIntSeconds(LocalDateTime datetime) {
Clock clock = Clock.systemDefaultZone();
ZoneOffset offset = clock.getZone().getRules().getOffset(clock.instant());
return (int) datetime.toEpochSecond(offset);
}
/**
* LocalDate转为时间秒
*
* @param date LocalDate
* @return 时间秒(10位)
*/
public static int toIntSeconds(LocalDate date) {
Clock clock = Clock.systemDefaultZone();
ZoneOffset offset = clock.getZone().getRules().getOffset(clock.instant());
return (int) date.toEpochSecond(LocalTime.MIN, offset);
}
/**
* 将时间字符串转化成LocalDateTime
*
* @param datetime "2020-12-14 10:56:11"
* @param pattern "yyyy-MM-dd HH:mm:ss"
* @return LocalDateTime
*/
public static LocalDateTime parse(String datetime, String pattern) {
return LocalDateTime.parse(datetime, DateTimeFormatter.ofPattern(pattern));
}
/**
* 根据传入的年份获取开始时间和结束时间的int位时间戳
*
* @param dateYear 年份日期字符串 如 2021
* @return int[]
*/
public static int[] getStartAndEndDate(String dateYear) {
String startDate = String.format("%s-01-01 00:00:00", dateYear);
String endDate = String.format("%s-12-31 23:59:59", dateYear);
int startInt = LocalDateTimeUtil.convertString2Seconds(startDate, "yyyy-MM-dd HH:mm:ss");
int endInt = LocalDateTimeUtil.convertString2Seconds(endDate, "yyyy-MM-dd HH:mm:ss");
int[] ints = new int[2];
ints[0] = startInt;
ints[1] = endInt;
return ints;
}
/**
* 将时间字符串转化成秒
*
* @param datetime 时间字符串 "2020-12-14 10:56:11"
* @param pattern 格式 "yyyy-MM-dd HH:mm:ss"
* @return int
*/
public static int convertString2Seconds(String datetime, String pattern) {
return toIntSeconds(parse(datetime, pattern));
}
/**
* 将时间字符串转换为秒
*
* @param datetime 时间字符串 "2021-05-26"
* @return "yyyy-MM-dd HH:mm:ss"格式数据
*/
public static int covertNMDToSeconds(String datetime) {
LocalDate localDate = LocalDate.parse(datetime, DateTimeFormatter.ofPattern(YYYY_MM_DD));
return toIntSeconds(localDate);
}