JSONDateMatchersEnum时间格式枚举类
package convert;
public enum JSONDateMatchersEnum {
DEFAULT_MATCHER("yyyy-MM-dd HH:MM:ss", "^((((1[6-9]|[2-9]\\d)\\d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]\\d|3[01]))|(((1[6-9]|[2-9]\\d)\\d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]\\d|30))|(((1[6-9]|[2-9]\\d)\\d{2})-0?2-(0?[1-9]|1\\d|2[0-8]))|(((1[6-9]|[2-9]\\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29-)) (20|21|22|23|[0-1]?\\d):[0-5]?\\d:[0-5]?\\d$");
private final String format;
private final String matcher;
private JSONDateMatchersEnum(String format, String matcher) {
this.format = format;
this.matcher = matcher;
}
public String getFormat() {
return format;
}
public String getMatcher() {
return matcher;
}
}
包含递归json子集的时间值,修改时间值的转换工具类
package convert;标签:fastjson,instanceof,String,JSONObject,value,json,子集,matcher,public From: https://www.cnblogs.com/meitanzai/p/17181243.html
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.Feature;
import com.alibaba.fastjson.serializer.SerializerFeature;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class JSONConvertUtils {
private JSONConvertUtils() {
}
public static String convertTimeZone(String jsonStr) {
JSONObject ret = convertTimeZone(JSONObject.parseObject(jsonStr, Feature.OrderedField));
return JSONObject.toJSONString(ret);
}
public static JSONObject convertTimeZone(JSONObject jsonObject) {
for (Map.Entry<String, Object> entry : jsonObject.entrySet()) {
Object value = entry.getValue();
if (value instanceof JSONArray) {
handleArray((JSONArray) value);
}
if (value instanceof JSONObject) {
handleJsonObject((JSONObject) value);
}
if (value instanceof String) {
String newValue = handleMatcherDateValue((String) value);
entry.setValue(newValue);
}
}
return jsonObject;
}
public static void handleArray(JSONArray array) {
for (int i = 0; i < array.size(); i++) {
Object object = array.get(i);
if (object instanceof JSONArray) {
handleArray((JSONArray) object);
}
if (object instanceof JSONObject) {
handleJsonObject((JSONObject) object);
}
if (object instanceof String) {
String newValue = handleMatcherDateValue((String) object);
array.set(i, newValue);
}
}
}
public static void handleJsonObject(JSONObject jsonObject) {
for (Map.Entry<String, Object> entry : jsonObject.entrySet()) {
Object value = entry.getValue();
if (value instanceof JSONArray) {
handleArray((JSONArray) value);
}
if (value instanceof JSONObject) {
handleJsonObject((JSONObject) value);
}
if (value instanceof String) {
String newValue = handleMatcherDateValue((String) value);
entry.setValue(newValue);
}
}
}
public static String handleMatcherDateValue(String value) {
for (JSONDateMatchersEnum enums : JSONDateMatchersEnum.values()) {
Pattern p = Pattern.compile(enums.getMatcher());
Matcher m = p.matcher(value);
if (m.matches()) {
//转换时区日期格式
//System.out.println(enums.getFormat() + " " + value);
return "2099-09-09 09:09:09";
}
}
return value;
}
// public static void main(String[] args) {
//
// String json = JSONObject.toJSONString(jsonStr, SerializerFeature.DisableCircularReferenceDetect);
// System.out.println("替换前的信息是 :\n" + json);
// System.out.println("---------------------------");
//
//
// String resultString = JSONConvertUtils.convertTimeZone(json);
// System.out.println("替换后的信息是 :\n" + resultString);
// System.out.println("---------------------------");
//
//
// Pattern pat = Pattern.compile("^((((1[6-9]|[2-9]\\d)\\d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]\\d|3[01]))|(((1[6-9]|[2-9]\\d)\\d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]\\d|30))|(((1[6-9]|[2-9]\\d)\\d{2})-0?2-(0?[1-9]|1\\d|2[0-8]))|(((1[6-9]|[2-9]\\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29-)) (20|21|22|23|[0-1]?\\d):[0-5]?\\d:[0-5]?\\d$");
// Matcher matcher = pat.matcher("2023-03-03 23:59:00");
// if (matcher.matches()) {
// System.out.println("时间格式-------:");
// }
//
// }
}