|
@@ -0,0 +1,55 @@
|
|
|
+package cn.fastfun.util;
|
|
|
+
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+
|
|
|
+import java.text.ParseException;
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
+import java.util.Date;
|
|
|
+
|
|
|
+@Slf4j
|
|
|
+public class DateUtils {
|
|
|
+
|
|
|
+ public static String YMD = "yyyy-MM-dd";
|
|
|
+
|
|
|
+ public static String YMDHMS = "yyyy-MM-dd HH:mm:ss";
|
|
|
+
|
|
|
+ public static Long getTime(String start, String end) {
|
|
|
+ try {
|
|
|
+ return (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(end).getTime() - new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(start).getTime()) / 1000;
|
|
|
+ } catch (ParseException e) {
|
|
|
+ log.error("时间格式错误:{}", e.getMessage());
|
|
|
+ }
|
|
|
+ return 0l;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String toDateTime(Long time) {
|
|
|
+ return toDateTime(time, "yyyy-MM-dd HH:mm:ss");
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String toString(Date time, String pattern) {
|
|
|
+ return new SimpleDateFormat(pattern).format(time);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static Date setDateTime(Date date, String hms) {
|
|
|
+ try {
|
|
|
+ return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(toDateTime(date.getTime(), "yyyy-MM-dd") + " " + hms);
|
|
|
+ } catch (ParseException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public static String toDateTime(Long time, String pattern) {
|
|
|
+ return new SimpleDateFormat(pattern).format(time);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static Date toDate(String date, String pattern) {
|
|
|
+ try {
|
|
|
+ return new SimpleDateFormat(pattern).parse(date);
|
|
|
+ } catch (ParseException e) {
|
|
|
+ log.error("时间转换异常: {}", e.getMessage());
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+}
|