public class LocalDateTimeUtils {
public static String format(LocalDateTime datetime, DateTimeFormatter formatter){
return formatter.format(datetime);
}
/**
* 對LocalDateTime進行指定格式的格式換並轉字串
* @param datetime
* @param pattern
* @return
*/
public static String format(LocalDateTime datetime,String pattern){
return DateTimeFormatter.ofPattern(pattern).format(datetime);
}
/**
* 得到LocalDateTime對應的時間單位秒
* @param datetime
* @return
*/
public static long getTotalSeconds(LocalDateTime datetime){
Clock clock = Clock.systemDefaultZone();
ZoneOffset offset = clock.getZone().getRules().getOffset(clock.instant());
return datetime.toEpochSecond(offset);
}
/**
*將時間字串轉化成LocalDateTime
* @param datetime "2020-12-14 10:56:11"
* @param pattern "yyyy-MM-dd HH:mm:ss"
* @return
*/
public static LocalDateTime parse(String datetime,String pattern){
return LocalDateTime.parse(datetime,DateTimeFormatter.ofPattern(pattern));
//LocalDateTimeStringConverter converter = new LocalDateTimeStringConverter();
}
/**
* 將時間轉化成秒
* @param datetime "2020-12-14 10:56:11"
* @param pattern "yyyy-MM-dd HH:mm:ss"
* @return
*/
public static long convertString2Seconds(String datetime,String pattern){
return getTotalSeconds(parse(datetime,pattern));
}
}