Skip to content

Commit

Permalink
up
Browse files Browse the repository at this point in the history
  • Loading branch information
youfanx committed Sep 14, 2023
1 parent 17513cc commit 5a9bf4e
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 10 deletions.
32 changes: 22 additions & 10 deletions rxlib/src/main/java/org/rx/bean/DateTime.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
import lombok.SneakyThrows;
import org.apache.commons.lang3.time.FastDateFormat;
import org.rx.annotation.ErrorCode;
import org.rx.core.Arrays;
import org.rx.core.Linq;
import org.rx.core.RxConfig;
import org.rx.exception.ApplicationException;

import java.text.ParseException;
Expand All @@ -15,6 +14,7 @@
import java.util.TimeZone;

import static org.rx.core.Constants.NON_UNCHECKED;
import static org.rx.core.Extends.ifNull;
import static org.rx.core.Extends.values;

/**
Expand All @@ -24,11 +24,14 @@
public final class DateTime extends Date {
private static final long serialVersionUID = 414744178681347341L;
public static final DateTime MIN = new DateTime(2000, 1, 1, 0, 0, 0), MAX = new DateTime(9999, 12, 31, 0, 0, 0);
static final String DATE_FORMAT = "yyy-MM-dd";
static final String TIME_FORMAT = "HH:mm:ss";
public static final String DATE_FORMAT = "yyy-MM-dd";
public static final String TIME_FORMAT = "HH:mm:ss";
public static final String DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
public static final String FULL_FORMAT = "yyyy-MM-dd HH:mm:ss,SSSZ";
public static final Linq<String> FORMATS = Linq.from(FULL_FORMAT, "yyyy-MM-dd HH:mm:ss,SSS", DATE_TIME_FORMAT, "yyyy-MM-dd'T'HH:mm:ss,SSSZ", "yyyyMMddHHmmssSSS");
public static final String ISO_DATE_TIME_FORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSSZ";
//2020-02-02 14:00:00.001 会适配 yyyy-MM-dd HH:mm:ss
public static final String[] FORMATS = new String[]{ISO_DATE_TIME_FORMAT, "yyyy-MM-dd HH:mm:ss.SSSZ", "yyyy-MM-dd HH:mm:ss.SSS",
DATE_TIME_FORMAT, DATE_FORMAT, TIME_FORMAT,
"yyyyMMddHHmmssSSS"};
static final TimeZone UTC_ZONE = TimeZone.getTimeZone("UTC");

public static DateTime now() {
Expand All @@ -44,11 +47,20 @@ public static DateTime utcNow() {
}

@ErrorCode(cause = ParseException.class)
public static DateTime valueOf(String dateString) {
public static DateTime valueOf(@NonNull String dateString) {
Throwable lastEx = null;
for (String format : Arrays.toList(DATE_TIME_FORMAT, "yyyy-MM-dd HH:mm:ss,SSS", FULL_FORMAT, "yyyyMMddHHmmssSSS")) {
int offset = dateString.length() >= 23 ? 0 : 3;
int len = 3, fb = 6;
for (int i = offset; i < len; i++) {
try {
return valueOf(dateString, format);
return valueOf(dateString, FORMATS[i]);
} catch (Throwable ex) {
lastEx = ex;
}
}
for (int i = fb; i < FORMATS.length; i++) {
try {
return valueOf(dateString, FORMATS[i]);
} catch (Throwable ex) {
lastEx = ex;
}
Expand Down Expand Up @@ -260,7 +272,7 @@ public String toDateTimeString() {

@Override
public String toString() {
return toString(FULL_FORMAT);
return toString(ifNull(RxConfig.INSTANCE.getDateFormat(), DATE_TIME_FORMAT));
}

public String toString(@NonNull String format) {
Expand Down
3 changes: 3 additions & 0 deletions rxlib/src/main/java/org/rx/core/RxConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ public interface ConfigNames {

String APP_ID = "app.id";
String MX_SAMPLING_PERIOD = "app.mxSamplingPeriod";
String DATE_FORMAT = "app.dateFormat";
String LOG_STRATEGY = "app.logStrategy";
String JSON_SKIP_TYPES = "app.jsonSkipTypes";
String AES_KEY = "app.aesKey";
Expand Down Expand Up @@ -188,6 +189,7 @@ public static class DnsConfig {
String aesKey;
String mxpwd;
long mxSamplingPeriod;
String dateFormat;
final Set<Class<?>> jsonSkipTypes = ConcurrentHashMap.newKeySet();
LogStrategy logStrategy;
final Set<String> logTypeWhitelist = ConcurrentHashMap.newKeySet();
Expand Down Expand Up @@ -271,6 +273,7 @@ public void refreshFromSystemProperty() {
aesKey = SystemPropertyUtil.get(ConfigNames.AES_KEY, aesKey);
mxpwd = SystemPropertyUtil.get(ConfigNames.MXPWD, mxpwd);
mxSamplingPeriod = SystemPropertyUtil.getLong(ConfigNames.MX_SAMPLING_PERIOD, mxSamplingPeriod);
dateFormat = SystemPropertyUtil.get(ConfigNames.DATE_FORMAT, dateFormat);
String v = SystemPropertyUtil.get(ConfigNames.JSON_SKIP_TYPES);
if (v != null) {
jsonSkipTypes.clear();
Expand Down
1 change: 1 addition & 0 deletions rxlib/src/main/resources/rx.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ app:
aesKey: ℞FREEDOM
mxpwd: rxlib
mxSamplingPeriod: 60000
dateFormat: yyyy-MM-dd HH:mm:ss
jsonSkipTypes:
- javax.servlet.ServletRequest
- javax.servlet.ServletResponse
Expand Down

0 comments on commit 5a9bf4e

Please sign in to comment.