2323import java .sql .Timestamp ;
2424import java .text .ParseException ;
2525import java .text .SimpleDateFormat ;
26+ import java .time .Instant ;
2627import java .time .LocalDate ;
27- import java .time .LocalDateTime ;
2828import java .time .LocalTime ;
29- import java .time .ZoneId ;
30- import java .time .format .DateTimeFormatter ;
31- import java .time .format .DateTimeParseException ;
29+ import java .time .ZoneOffset ;
3230import java .util .Calendar ;
3331import java .util .Date ;
3432import java .util .Locale ;
3533import java .util .SimpleTimeZone ;
34+ import java .util .TimeZone ;
35+ import java .util .regex .Pattern ;
36+
37+ import static java .time .format .DateTimeFormatter .ISO_INSTANT ;
3638
3739
3840/**
4547 */
4648public class DateUtil {
4749
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" );
50+ static final String timeZone = "GMT+8" ;
51+ static final String datetimeFormat = "yyyy-MM-dd HH:mm:ss" ;
52+ static final String dateFormat = "yyyy-MM-dd" ;
53+ static final String timeFormat = "HH:mm:ss" ;
54+ static final SimpleDateFormat datetimeFormatter = new SimpleDateFormat (datetimeFormat );
55+ static final SimpleDateFormat dateFormatter = new SimpleDateFormat (dateFormat );
56+ static final SimpleDateFormat timeFormatter = new SimpleDateFormat (timeFormat );
57+
58+ private static final Pattern DATETIME = Pattern .compile ("^\\ d{4}-(?:0[0-9]|1[0-2])-[0-9]{2}T\\ d{2}:\\ d{2}:\\ d{2}(\\ .\\ d{3,9})?Z$" );
59+ private static final Pattern DATE = Pattern .compile ("^\\ d{4}-(?:0[0-9]|1[0-2])-[0-9]{2}$" );
60+
61+ private static final int MILLIS_PER_SECOND = 1000 ;
62+
5163
5264 public static java .sql .Date columnToDate (Object column ) {
5365 if (column instanceof String ) {
@@ -72,32 +84,23 @@ public static Date stringToDate(String strDate) {
7284 return null ;
7385 }
7486 try {
75- ;
76- return localDateTimetoDate (LocalDateTime .parse (strDate , DATE_TIME_FORMATTER ));
77- } catch (DateTimeParseException ignored ) {
87+ return datetimeFormatter .parse (strDate );
88+ } catch (ParseException ignored ) {
7889 }
7990
8091 try {
81- return localDateTimetoDate ( LocalDate .parse (strDate , DATE_FORMATTER ). atStartOfDay () );
82- } catch (DateTimeParseException ignored ) {
92+ return dateFormatter .parse (strDate );
93+ } catch (ParseException ignored ) {
8394 }
8495
8596 try {
86- return localDateTimetoDate ( LocalDateTime . of ( LocalDate . now (), LocalTime . parse (strDate , TIME_FORMATTER )) );
87- } catch (DateTimeParseException ignored ) {
97+ return timeFormatter . parse (strDate );
98+ } catch (ParseException ignored ) {
8899 }
89100
90101 throw new RuntimeException ("can't parse date" );
91102 }
92103
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-
101104 /**
102105 *
103106 *
@@ -127,9 +130,9 @@ public static long getTodayStart(long day) {
127130 * @return
128131 */
129132 public static long getTodayStart (long day ,String scope ) {
130- if ("MS" .equals (scope )){
133+ if (scope .equals ("MS" )){
131134 return getTodayStart (day )*1000 ;
132- }else if ("S" .equals (scope )){
135+ }else if (scope .equals ("S" )){
133136 return getTodayStart (day );
134137 }else {
135138 return getTodayStart (day );
@@ -165,9 +168,9 @@ public static long getNextDayStart(long day) {
165168 * @return
166169 */
167170 public static long getNextDayStart (long day ,String scope ) {
168- if ("MS" .equals (scope )){
171+ if (scope .equals ("MS" )){
169172 return getNextDayStart (day )*1000 ;
170- }else if ("S" .equals (scope )){
173+ }else if (scope .equals ("S" )){
171174 return getNextDayStart (day );
172175 }else {
173176 return getNextDayStart (day );
@@ -346,7 +349,7 @@ public static String get30DaysLaterByString(String day, String inFormat, String
346349 * @return String
347350 * @throws ParseException
348351 */
349- public static String getDateStrToFormat (String day , String inFormat , String outFormat ) throws ParseException {
352+ public static String getDateStrTOFormat (String day , String inFormat , String outFormat ) throws ParseException {
350353 SimpleDateFormat sdf = new SimpleDateFormat (inFormat );
351354 Date date = sdf .parse (day );
352355 Calendar calendar = Calendar .getInstance ();
@@ -355,7 +358,7 @@ public static String getDateStrToFormat(String day, String inFormat, String outF
355358 return dayBefore ;
356359 }
357360
358- public static long getDateMillToFormat (String day , String inFormat ) throws ParseException {
361+ public static long getDateMillTOFormat (String day , String inFormat ) throws ParseException {
359362 SimpleDateFormat sdf = new SimpleDateFormat (inFormat );
360363 Date date = sdf .parse (day );
361364 Calendar calendar = Calendar .getInstance ();
@@ -481,11 +484,11 @@ public static long getMillByDay(int severalDays,String condition) {
481484 if (condition ==null ){
482485 return getMillToDay (cal ,dateT );
483486 }
484- if ("-" .equals (condition )){
487+ if (condition .equals ("-" )){
485488 dateT = (cal .get (Calendar .DATE ) - severalDays );
486489 return getMillToDay (cal ,dateT );
487490 }
488- if ("+" .equals (condition )){
491+ if (condition .equals ("+" )){
489492 dateT = (cal .get (Calendar .DATE ) + severalDays );
490493 return getMillToDay (cal ,dateT );
491494 }
@@ -501,11 +504,11 @@ public static long getStampByDay(int severalDays,String condition) {
501504 if (condition ==null ){
502505 return getStampToDay (cal ,dateT );
503506 }
504- if ("-" .equals (condition )){
507+ if (condition .equals ("-" )){
505508 dateT = (cal .get (Calendar .DATE ) - severalDays );
506509 return getStampToDay (cal ,dateT );
507510 }
508- if ("+" .equals (condition )){
511+ if (condition .equals ("+" )){
509512 dateT = (cal .get (Calendar .DATE ) + severalDays );
510513 return getStampToDay (cal ,dateT );
511514 }
@@ -586,8 +589,8 @@ public static String getDate(Date date, String format) {
586589 */
587590 public static long stringToLong (String day , String format ) throws ParseException {
588591 SimpleDateFormat dateFormat = new SimpleDateFormat (format );
589- long date = dateFormat .parse (day ).getTime ();
590- return date ;
592+ long Date = dateFormat .parse (day ).getTime ();
593+ return Date ;
591594 }
592595
593596 /**
@@ -599,8 +602,8 @@ public static long stringToLong(String day, String format) throws ParseException
599602 public static Date stringToDate (String day , String format ) {
600603 try {
601604 SimpleDateFormat dateFormat = new SimpleDateFormat (format );
602- Date date = dateFormat .parse (day );
603- return date ;
605+ Date Date = dateFormat .parse (day );
606+ return Date ;
604607 } catch (ParseException e ) {
605608 return new Date ();
606609 }
@@ -619,8 +622,8 @@ public static String longToString(long day, String format) throws ParseException
619622 day =day *1000 ;
620623 }
621624 SimpleDateFormat dateFormat = new SimpleDateFormat (format );
622- String date = dateFormat .format (day );
623- return date ;
625+ String Date = dateFormat .format (day );
626+ return Date ;
624627 }
625628
626629 /**
@@ -774,12 +777,59 @@ public static java.sql.Timestamp columnToTimestamp(Object column) {
774777 }
775778
776779 public static String dateToString (Date date ) {
777- LocalDateTime localDateTime = dateToLocalDateTime (date );
778- return localDateTime .format (DATE_FORMATTER );
780+ return dateFormatter .format (date );
779781 }
780782
781783 public static String timestampToString (Date date ) {
782- LocalDateTime localDateTime = dateToLocalDateTime (date );
783- return localDateTime .format (DATE_TIME_FORMATTER );
784+ return datetimeFormatter .format (date );
784785 }
786+
787+
788+ public static Timestamp getTimestampFromStr (String timeStr ) {
789+ if (DATETIME .matcher (timeStr ).matches ()) {
790+ Instant instant = Instant .from (ISO_INSTANT .parse (timeStr ));
791+ return new Timestamp (instant .getEpochSecond () * MILLIS_PER_SECOND );
792+ } else {
793+ java .sql .Date date = null ;
794+ try {
795+ date = new java .sql .Date (datetimeFormatter .parse (timeStr ).getTime ());
796+ } catch (ParseException e ) {
797+ throw new RuntimeException ("getTimestampFromStr error data is " + timeStr );
798+ }
799+ return new Timestamp (date .getTime ());
800+ }
801+ }
802+
803+ public static java .sql .Date getDateFromStr (String dateStr ) {
804+ // 2020-01-01 format
805+ if (DATE .matcher (dateStr ).matches ()) {
806+ // convert from local date to instant
807+ Instant instant = LocalDate .parse (dateStr ).atTime (LocalTime .of (0 , 0 , 0 , 0 )).toInstant (ZoneOffset .UTC );
808+ // calculate the timezone offset in millis
809+ int offset = TimeZone .getDefault ().getOffset (instant .toEpochMilli ());
810+ // need to remove the offset since time has no TZ component
811+ return new java .sql .Date (instant .toEpochMilli () - offset );
812+ } else if (DATETIME .matcher (dateStr ).matches ()) {
813+ // 2020-01-01T12:12:12Z format
814+ Instant instant = Instant .from (ISO_INSTANT .parse (dateStr ));
815+ return new java .sql .Date (instant .toEpochMilli ());
816+ } else {
817+ try {
818+ // 2020-01-01 12:12:12.0 format
819+ return new java .sql .Date (datetimeFormatter .parse (dateStr ).getTime ());
820+ } catch (ParseException e ) {
821+ throw new RuntimeException ("String convert to Date fail." );
822+ }
823+ }
824+ }
825+
826+
827+ public static String getStringFromTimestamp (Timestamp timestamp ) {
828+ return datetimeFormatter .format (timestamp );
829+ }
830+
831+ public static String getStringFromDate (java .sql .Date date ) {
832+ return dateFormatter .format (date );
833+ }
834+
785835}
0 commit comments