java时间封装
java时间封装
public class LocalDateUtil {
/**
* 取当前时间戳
* @param
* @return yyyy-MM-dd HH:mm:ss
*/
public static long getCurrentTimestamp(){
Long millisecond = Instant.now().toEpochMilli(); // 精确到毫秒
Long second = Instant.now().getEpochSecond();// 精确到秒
return millisecond;
}
/**
* 取当前时间戳
* @param
* @return yyyy-MM-dd HH:mm:ss
*/
public static String getCurrentString(){
LocalDateTime localDateTime=LocalDateTime.now();
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
return localDateTime.format(dateTimeFormatter);
}
/**
* 字符串转换datetime
* @param dateTime
* @return yyyy-MM-dd HH:mm:ss
*/
public static Date stringCoverDateTime(String dateTime){
LocalDateTime startDateTime = LocalDateTime.parse(dateTime, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
Date LocalDateTimeToDate = Date.from(startDateTime.atZone(ZoneId.systemDefault()).toInstant());
return LocalDateTimeToDate;
}
/**
* 字符串转换date
* @param dateTime
* @return yyyy-MM-dd
*/
public static Date stringCoverDate(String dateTime){
LocalDateTime startDateTime =
LocalDateTime.parse(dateTime, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
Date LocalDateTimeToDate = Date.from(startDateTime.atZone(ZoneId.systemDefault()).toInstant());
return LocalDateTimeToDate;
}
/**
* 将LocalDateTime转为自定义的时间格式的字符串
* @param localDateTime,format
* @return yyyy-MM-dd
*/
public static String getDateTimeAsString(LocalDateTime localDateTime, String format) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(format);
return localDateTime.format(formatter);
}
/**
* 将long类型的timestamp转为LocalDateTime
* @param timestamp
* @return yyyy-MM-dd
*/
public static LocalDateTime getDateTimeOfTimestamp(long timestamp) {
Instant instant = Instant.ofEpochMilli(timestamp);
ZoneId zone = ZoneId.systemDefault();
return LocalDateTime.ofInstant(instant, zone);
}
/**
* 将LocalDateTime转为long类型的timestamp
* @param localDateTime
* @return yyyy-MM-dd
*/
public static long getTimestampOfDateTime(LocalDateTime localDateTime) {
ZoneId zone = ZoneId.systemDefault();
Instant instant = localDateTime.atZone(zone).toInstant();
return instant.toEpochMilli();
}
/**
* 将字符串转日期成Long类型的时间戳,格式为:yyyy-MM-dd HH:mm:ss
* @param time
* @return yyyy-MM-dd
*/
public static long timeToLong(String time) {
Assert.notNull(time, "time is null");
DateTimeFormatter ftf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDateTime parse = LocalDateTime.parse("2019-11-28 08:52:50", ftf);
return LocalDateTime.from(parse).atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
}
/**
* 将Long类型的时间戳转成字符串
* @param time
* @return yyyy-MM-dd
*/
public static String timeToString(Long time){
Assert.notNull(time, "time is null");
DateTimeFormatter ftf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
return ftf.format(LocalDateTime.ofInstant(Instant.ofEpochMilli(time),ZoneId.systemDefault()));
}
/**
* 将某时间字符串转为自定义时间格式的LocalDateTime
* @param time,format
* @return yyyy-MM-dd
*/
public static LocalDateTime parseStringToDateTime(String time, String format) {
DateTimeFormatter df = DateTimeFormatter.ofPattern(format);
return LocalDateTime.parse(time, df);
}
/**
* 比较第一个日期是否小于第二个日期
* @param firstDate 第一个日期
* @param secondDate 第二个日期
* @return true-小于;false-大于
*/
public static boolean localDateIsBefore(LocalDate firstDate, LocalDate secondDate) {
return firstDate.isBefore(secondDate);
}
/**
* 比较第一个日期是否大于第二个日期
* @param firstDate 第一个日期
* @param secondDate 第二个日期
* @return true-大于;false-不大于
*/
public static boolean localDateIsAfter(LocalDate firstDate, LocalDate secondDate) {
return firstDate.isAfter(secondDate);
}
/**
* 比较两个日期是否相等
* @param firstDate 第一个日期
* @param secondDate 第二个日期
* @return true-相等;false-不相等
*/
public static boolean localDateIsEqual(LocalDate firstDate, LocalDate secondDate) {
return firstDate.isEqual(secondDate);
}
}

