Skip to content

Commit 78b3a1a

Browse files
author
dapeng
committed
时间格式化
1 parent 2a0663e commit 78b3a1a

File tree

1 file changed

+28
-15
lines changed

1 file changed

+28
-15
lines changed

core/src/main/java/com/dtstack/flink/sql/util/DateUtil.java

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@
2323
import java.sql.Timestamp;
2424
import java.text.ParseException;
2525
import java.text.SimpleDateFormat;
26+
import java.time.LocalDate;
27+
import java.time.LocalDateTime;
28+
import java.time.LocalTime;
29+
import java.time.ZoneId;
30+
import java.time.format.DateTimeFormatter;
31+
import java.time.format.DateTimeParseException;
2632
import java.util.Calendar;
2733
import java.util.Date;
2834
import java.util.Locale;
@@ -39,12 +45,9 @@
3945
*/
4046
public class DateUtil {
4147

42-
static final String datetimeFormat = "yyyy-MM-dd HH:mm:ss";
43-
static final String dateFormat = "yyyy-MM-dd";
44-
static final String timeFormat = "HH:mm:ss";
45-
static final SimpleDateFormat datetimeFormatter = new SimpleDateFormat(datetimeFormat);
46-
static final SimpleDateFormat dateFormatter = new SimpleDateFormat(dateFormat);
47-
static final SimpleDateFormat timeFormatter = new SimpleDateFormat(timeFormat);
48+
static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
49+
static final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd");
50+
static final DateTimeFormatter TIME_FORMATTER = DateTimeFormatter.ofPattern("HH:mm:ss");
4851

4952
public static java.sql.Date columnToDate(Object column) {
5053
if(column instanceof String) {
@@ -69,23 +72,32 @@ public static Date stringToDate(String strDate) {
6972
return null;
7073
}
7174
try {
72-
return datetimeFormatter.parse(strDate);
73-
} catch (ParseException ignored) {
75+
;
76+
return localDateTimetoDate(LocalDateTime.parse(strDate, DATE_TIME_FORMATTER));
77+
} catch (DateTimeParseException ignored) {
7478
}
7579

7680
try {
77-
return dateFormatter.parse(strDate);
78-
} catch (ParseException ignored) {
81+
return localDateTimetoDate(LocalDate.parse(strDate, DATE_FORMATTER).atStartOfDay());
82+
} catch (DateTimeParseException ignored) {
7983
}
8084

8185
try {
82-
return timeFormatter.parse(strDate);
83-
} catch (ParseException ignored) {
86+
return localDateTimetoDate(LocalDateTime.of(LocalDate.now(), LocalTime.parse(strDate, TIME_FORMATTER)));
87+
} catch (DateTimeParseException ignored) {
8488
}
8589

8690
throw new RuntimeException("can't parse date");
8791
}
8892

93+
public static Date localDateTimetoDate(LocalDateTime localDateTime){
94+
return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
95+
}
96+
97+
public static LocalDateTime dateToLocalDateTime(Date date){
98+
return date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
99+
}
100+
89101
/**
90102
*
91103
*
@@ -762,11 +774,12 @@ public static java.sql.Timestamp columnToTimestamp(Object column) {
762774
}
763775

764776
public static String dateToString(Date date) {
765-
return dateFormatter.format(date);
777+
LocalDateTime localDateTime = dateToLocalDateTime(date);
778+
return localDateTime.format(DATE_FORMATTER);
766779
}
767780

768781
public static String timestampToString(Date date) {
769-
return datetimeFormatter.format(date);
782+
LocalDateTime localDateTime = dateToLocalDateTime(date);
783+
return localDateTime.format(DATE_TIME_FORMATTER);
770784
}
771-
772785
}

0 commit comments

Comments
 (0)