[fix](fe) Fx SimpleDateFormatter thread unsafe issue by replacing to DateTimeFormatter. (#19265)
DateTimeFormatter replace SimpleDateFormat in fe module because SimpleDateFormat is not thread-safe.
This commit is contained in:
@ -25,8 +25,10 @@ import com.google.common.collect.ImmutableMap;
|
||||
import java.io.DataInput;
|
||||
import java.io.DataOutput;
|
||||
import java.io.IOException;
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.ZoneId;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.time.format.DateTimeParseException;
|
||||
import java.util.Arrays;
|
||||
import java.util.Date;
|
||||
import java.util.Optional;
|
||||
@ -224,14 +226,14 @@ public abstract class PropertySchema<T> {
|
||||
}
|
||||
|
||||
public static final class DateProperty extends PropertySchema<Date> {
|
||||
SimpleDateFormat dateFormat;
|
||||
DateTimeFormatter dateFormat;
|
||||
|
||||
public DateProperty(String name, SimpleDateFormat dateFormat) {
|
||||
public DateProperty(String name, DateTimeFormatter dateFormat) {
|
||||
super(name);
|
||||
this.dateFormat = dateFormat;
|
||||
}
|
||||
|
||||
DateProperty(String name, SimpleDateFormat dateFormat, boolean isRequired) {
|
||||
DateProperty(String name, DateTimeFormatter dateFormat, boolean isRequired) {
|
||||
super(name, isRequired);
|
||||
this.dateFormat = dateFormat;
|
||||
}
|
||||
@ -266,15 +268,15 @@ public abstract class PropertySchema<T> {
|
||||
|
||||
public Date readTimeFormat(String timeStr) throws IllegalArgumentException {
|
||||
try {
|
||||
return this.dateFormat.parse(timeStr);
|
||||
} catch (ParseException e) {
|
||||
return Date.from(LocalDateTime.parse(timeStr, dateFormat).atZone(ZoneId.systemDefault()).toInstant());
|
||||
} catch (DateTimeParseException e) {
|
||||
throw new IllegalArgumentException("Invalid time format, time param need "
|
||||
+ "to be " + this.dateFormat.toPattern());
|
||||
+ "to be " + this.dateFormat.toString());
|
||||
}
|
||||
}
|
||||
|
||||
public String writeTimeFormat(Date timeDate) throws IllegalArgumentException {
|
||||
return this.dateFormat.format(timeDate.getTime());
|
||||
return LocalDateTime.ofInstant(timeDate.toInstant(), ZoneId.systemDefault()).format(this.dateFormat);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -31,7 +31,9 @@ import java.io.DataInput;
|
||||
import java.io.DataInputStream;
|
||||
import java.io.DataOutput;
|
||||
import java.io.DataOutputStream;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.ZoneId;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.Date;
|
||||
|
||||
public class PropertySchemaTest {
|
||||
@ -220,13 +222,16 @@ public class PropertySchemaTest {
|
||||
|
||||
@Test
|
||||
public void testDatePropNormal() throws Exception {
|
||||
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
||||
DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").withZone(ZoneId.systemDefault());
|
||||
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
|
||||
DataOutput output = new DataOutputStream(outStream);
|
||||
|
||||
PropertySchema.DateProperty prop =
|
||||
new PropertySchema.DateProperty("key", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
|
||||
prop.write(dateFormat.parse("2021-06-30 20:34:51"), output);
|
||||
new PropertySchema.DateProperty("key",
|
||||
DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").withZone(ZoneId.systemDefault()));
|
||||
prop.write(Date.from(
|
||||
LocalDateTime.parse("2021-06-30 20:34:51", dateFormat).atZone(ZoneId.systemDefault()).toInstant()),
|
||||
output);
|
||||
|
||||
ByteArrayInputStream inStream = new ByteArrayInputStream(outStream.toByteArray());
|
||||
DataInput input = new DataInputStream(inStream);
|
||||
@ -244,8 +249,9 @@ public class PropertySchemaTest {
|
||||
exceptionRule.expect(IllegalArgumentException.class);
|
||||
exceptionRule.expectMessage(Matchers.containsString("Invalid time format"));
|
||||
|
||||
PropertySchema.DateProperty prop = new PropertySchema.DateProperty("key", new SimpleDateFormat("yyyy-MM-dd "
|
||||
+ "HH:mm:ss"));
|
||||
PropertySchema.DateProperty prop = new PropertySchema.DateProperty("key",
|
||||
DateTimeFormatter.ofPattern("yyyy-MM-dd "
|
||||
+ "HH:mm:ss").withZone(ZoneId.systemDefault()));
|
||||
prop.read("2021-06-30");
|
||||
}
|
||||
|
||||
@ -254,8 +260,9 @@ public class PropertySchemaTest {
|
||||
exceptionRule.expect(IllegalArgumentException.class);
|
||||
exceptionRule.expectMessage(Matchers.containsString("Invalid time format"));
|
||||
|
||||
PropertySchema.DateProperty prop = new PropertySchema.DateProperty("key", new SimpleDateFormat("yyyy-MM-dd "
|
||||
+ "HH:mm:ss"));
|
||||
PropertySchema.DateProperty prop = new PropertySchema.DateProperty("key",
|
||||
DateTimeFormatter.ofPattern("yyyy-MM-dd "
|
||||
+ "HH:mm:ss").withZone(ZoneId.systemDefault()));
|
||||
prop.read((String) null);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user