Commit b285b4b8 authored by 李灿灿's avatar 李灿灿

字符串格式的日期转LocalDate工具类调整

parent 042086ce
...@@ -7,6 +7,7 @@ import java.text.DateFormat; ...@@ -7,6 +7,7 @@ import java.text.DateFormat;
import java.text.ParseException; import java.text.ParseException;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.time.LocalDate; import java.time.LocalDate;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter; import java.time.format.DateTimeFormatter;
import java.util.Calendar; import java.util.Calendar;
import java.util.Date; import java.util.Date;
...@@ -75,11 +76,14 @@ public class LocalDateUtil { ...@@ -75,11 +76,14 @@ public class LocalDateUtil {
*/ */
public static LocalDate parseLocalDate(String strDate){ public static LocalDate parseLocalDate(String strDate){
try { try {
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(NORM_DATE_PATTERN, Locale.CHINA); if (isDate(strDate,NORM_DATE_PATTERN)){
LocalDate localDate = LocalDate.parse(strDate, dateTimeFormatter); DateFormat dateFormat = new SimpleDateFormat(NORM_DATE_PATTERN);
return localDate; Date date = dateFormat.parse(strDate);
return date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
}
return null;
}catch (Exception e){ }catch (Exception e){
e.printStackTrace(); System.out.println("字符串格式的日期转LocalDate失败");
return null; return null;
} }
} }
...@@ -337,6 +341,6 @@ public class LocalDateUtil { ...@@ -337,6 +341,6 @@ public class LocalDateUtil {
} }
public static void main(String[] args) { public static void main(String[] args) {
System.out.println(isOverdueDate("2022-08-03")); System.out.println(parseLocalDate("2022-8-3"));
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment