mirror of
https://gitee.com/jzsw-it/yexuejc-base.git
synced 2025-06-25 06:22:41 +08:00
1.4.2
优化时间转换格式 增加注解 ToUeProperty 转换时间为时间戳的功能
This commit is contained in:
parent
1711fc8a0d
commit
2cdcf4fc7c
2
pom.xml
2
pom.xml
@ -6,7 +6,7 @@
|
||||
|
||||
<groupId>top.yexuejc</groupId>
|
||||
<artifactId>yexuejc-base</artifactId>
|
||||
<version>1.4.1</version>
|
||||
<version>1.4.2</version>
|
||||
|
||||
<name>${project.artifactId}</name>
|
||||
<url>https://github.com/yexuejc/yexuejc-base</url>
|
||||
|
@ -1,9 +1,9 @@
|
||||
package com.yexuejc.base.pojo;
|
||||
|
||||
import com.yexuejc.base.util.JsonUtil;
|
||||
import jakarta.validation.constraints.Min;
|
||||
import jakarta.validation.constraints.NotNull;
|
||||
|
||||
import javax.validation.constraints.Min;
|
||||
import javax.validation.constraints.NotNull;
|
||||
|
||||
/**
|
||||
* 分页 VO
|
||||
|
@ -13,6 +13,10 @@ import java.util.Date;
|
||||
* @date: 2018/3/27 10:44
|
||||
*/
|
||||
public class DateTimeUtil {
|
||||
public static String DATE_PATTERN = "yyyy-MM-dd";
|
||||
public static String DATE_TIME_PATTERN = "yyyy-MM-dd HH:mm:ss";
|
||||
public static String DATE_TIME_MS_PATTERN = "yyyy-MM-dd HH:mm:ss.SSS";
|
||||
|
||||
private DateTimeUtil() {
|
||||
}
|
||||
|
||||
@ -281,6 +285,45 @@ public class DateTimeUtil {
|
||||
Instant instant = localDateTime.atZone(zone).toInstant();
|
||||
return instant.toEpochMilli();
|
||||
}
|
||||
|
||||
/**
|
||||
* LocalDate 转 Long
|
||||
*
|
||||
* @param localDate
|
||||
* @return 13位(毫秒)最后3位为0
|
||||
*/
|
||||
public static long parseLong(LocalDate localDate) {
|
||||
ZoneId zone = ZoneId.systemDefault();
|
||||
Instant instant = localDate.atStartOfDay(zone).toInstant();
|
||||
return instant.toEpochMilli();
|
||||
}
|
||||
|
||||
/**
|
||||
* 格式化时间 <br/>
|
||||
* 格式 yyyy-MM-dd HH:mm:ss
|
||||
*
|
||||
* @param dateTime
|
||||
* @return
|
||||
*/
|
||||
public static String format(LocalDate dateTime) {
|
||||
return format(dateTime, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* 格式化时间
|
||||
*
|
||||
* @param dateTime
|
||||
* @param pattern 格式 默认:yyyy-MM-dd
|
||||
* @return
|
||||
*/
|
||||
public static String format(LocalDate dateTime, String pattern) {
|
||||
if (StrUtil.isEmpty(pattern)) {
|
||||
pattern = DATE_PATTERN;
|
||||
}
|
||||
DateTimeFormatter df = DateTimeFormatter.ofPattern(pattern);
|
||||
return df.format(dateTime);
|
||||
}
|
||||
|
||||
/**
|
||||
* 格式化时间 <br/>
|
||||
* 格式 yyyy-MM-dd HH:mm:ss
|
||||
@ -300,8 +343,8 @@ public class DateTimeUtil {
|
||||
* @return
|
||||
*/
|
||||
public static String format(LocalDateTime dateTime, String pattern) {
|
||||
if (pattern.isEmpty()) {
|
||||
pattern = "yyyy-MM-dd HH:mm:ss";
|
||||
if (StrUtil.isEmpty(pattern)) {
|
||||
pattern = DATE_TIME_PATTERN;
|
||||
}
|
||||
DateTimeFormatter df = DateTimeFormatter.ofPattern(pattern);
|
||||
return df.format(dateTime);
|
||||
|
@ -21,7 +21,7 @@ public class DateUtil {
|
||||
|
||||
public static DateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd");
|
||||
public static DateFormat TIME_FORMAT = new SimpleDateFormat("HH:mm:ss");
|
||||
public static DateFormat DATE_TIME_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
||||
public static DateFormat DATE_TIME_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
|
||||
public static String DAY_START_TIME = "00:00:00";
|
||||
public static String DAY_END_TIME = "23:59:59";
|
||||
|
||||
|
@ -2,7 +2,8 @@ package com.yexuejc.base.util;
|
||||
|
||||
import java.io.*;
|
||||
import java.lang.reflect.Field;
|
||||
import java.sql.Timestamp;
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
@ -110,6 +111,7 @@ public class ObjUtil {
|
||||
}
|
||||
boolean annotationPresent = f.isAnnotationPresent(ToUeProperty.class);
|
||||
boolean ignore = false;
|
||||
Class<?> toType = null;
|
||||
if (annotationPresent) {
|
||||
ToUeProperty annotation = f.getAnnotation(ToUeProperty.class);
|
||||
ignore = annotation.ignore();
|
||||
@ -117,17 +119,57 @@ public class ObjUtil {
|
||||
if (StrUtil.isNotEmpty(value)) {
|
||||
fName = value;
|
||||
}
|
||||
if (!annotation.type().getClass().equals(ObjUtil.class)) {
|
||||
toType = annotation.type();
|
||||
}
|
||||
}
|
||||
//忽略
|
||||
if (ignore) {
|
||||
continue;
|
||||
}
|
||||
Object o = f.get(obj);
|
||||
if (null == o && !putNull) {
|
||||
continue;
|
||||
}
|
||||
if (null == o || isPrimitive(o) || o instanceof String || o instanceof Enum) {
|
||||
if (null == o && !putNull) {
|
||||
continue;
|
||||
objMap.put(fName, o);
|
||||
} else if (o instanceof Date) {
|
||||
if (toType != null) {
|
||||
Date date = (Date) o;
|
||||
if (toType.equals(Integer.class)) {
|
||||
objMap.put(fName, (int) date.getTime() / 1000);
|
||||
} else if (toType.equals(Long.class)) {
|
||||
objMap.put(fName, date.getTime());
|
||||
}
|
||||
} else {
|
||||
objMap.put(fName, o);
|
||||
}
|
||||
} else if (o instanceof LocalDate) {
|
||||
if (toType != null) {
|
||||
LocalDate date = (LocalDate) o;
|
||||
if (toType.equals(Integer.class)) {
|
||||
objMap.put(fName, (int) DateTimeUtil.parseLong(date) / 1000);
|
||||
} else if (toType.equals(Long.class)) {
|
||||
objMap.put(fName, DateTimeUtil.parseLong(date));
|
||||
} else if (toType.equals(String.class)) {
|
||||
objMap.put(fName, DateTimeUtil.format(date));
|
||||
}
|
||||
} else {
|
||||
objMap.put(fName, o);
|
||||
}
|
||||
} else if (o instanceof LocalDateTime) {
|
||||
if (toType != null) {
|
||||
LocalDateTime date = (LocalDateTime) o;
|
||||
if (toType.equals(Integer.class)) {
|
||||
objMap.put(fName, (int) DateTimeUtil.parseLong(date) / 1000);
|
||||
} else if (toType.equals(Long.class)) {
|
||||
objMap.put(fName, DateTimeUtil.parseLong(date));
|
||||
} else if (toType.equals(String.class)) {
|
||||
objMap.put(fName, DateTimeUtil.format(date));
|
||||
}
|
||||
} else {
|
||||
objMap.put(fName, o);
|
||||
}
|
||||
objMap.put(fName, f.get(obj));
|
||||
} else if (o instanceof List) {
|
||||
List list = (List) o;
|
||||
List bodyList = new ArrayList();
|
||||
@ -181,80 +223,13 @@ public class ObjUtil {
|
||||
boolean b = obj.getClass().isPrimitive()
|
||||
|| obj instanceof Integer || obj instanceof Character || obj instanceof Boolean
|
||||
|| obj instanceof Number || obj instanceof String || obj instanceof Double || obj instanceof Float
|
||||
|| obj instanceof Short || obj instanceof Long || obj instanceof Byte || obj instanceof Date;
|
||||
|| obj instanceof Short || obj instanceof Long || obj instanceof Byte;
|
||||
if (b) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
B a = new B();
|
||||
a.nameFirst = "张三";
|
||||
a.ageInt = "5165458";
|
||||
a.testAss = "asdasdsad";
|
||||
a.setaM1("method1");
|
||||
a.setbM1("b1Mthod1");
|
||||
a.protectedStr = "protectedStr";
|
||||
C c = new C();
|
||||
c.ageInt = "test";
|
||||
a.c = c;
|
||||
a.list = new ArrayList<>();
|
||||
a.list.add(c);
|
||||
Map<String, Object> underlineMap = getUnderlineMap(a, false, false);
|
||||
System.out.println(JsonUtil.obj2Json(underlineMap));
|
||||
}
|
||||
|
||||
static class A implements Serializable {
|
||||
private static final long serialVersionUID = -8462118058721865488L;
|
||||
public String nameFirst;
|
||||
public String ageInt;
|
||||
private String aM1;
|
||||
@ToUeProperty("p_str")
|
||||
protected String protectedStr;
|
||||
|
||||
public String getaM1() {
|
||||
return aM1;
|
||||
}
|
||||
|
||||
public A setaM1(String aM1) {
|
||||
this.aM1 = aM1;
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
static class B extends A {
|
||||
private static final long serialVersionUID = -8462118058721865488L;
|
||||
public String testAss;
|
||||
private String bM1;
|
||||
private C c;
|
||||
List<C> list;
|
||||
|
||||
public String getbM1() {
|
||||
return bM1;
|
||||
}
|
||||
|
||||
public B setbM1(String bM1) {
|
||||
this.bM1 = bM1;
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
static class C extends A {
|
||||
private static final long serialVersionUID = -8462118058721865488L;
|
||||
public String testAss;
|
||||
private String bM1;
|
||||
|
||||
public String getbM1() {
|
||||
return bM1;
|
||||
}
|
||||
|
||||
public C setbM1(String bM1) {
|
||||
this.bM1 = bM1;
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* <h2>深度克隆对象</h2>
|
||||
* <p>
|
||||
@ -283,56 +258,4 @@ public class ObjUtil {
|
||||
return outer;
|
||||
}
|
||||
|
||||
// public static void main(String[] args) {
|
||||
//// test1();
|
||||
//// test2();
|
||||
//
|
||||
// }
|
||||
//
|
||||
// private static void test2() {
|
||||
// B t = new B();
|
||||
// t.sex = "男";
|
||||
// t.age = 18;
|
||||
// A test = new A();
|
||||
// test.name = "张三";
|
||||
// t.test = test;
|
||||
// B b = depthClone(t);
|
||||
// System.out.println(JsonUtil.obj2Json(b));
|
||||
// }
|
||||
//
|
||||
// static class A implements Serializable {
|
||||
// private static final long serialVersionUID = -8462118058721865488L;
|
||||
// public String name;
|
||||
// }
|
||||
//
|
||||
// static class B implements Serializable {
|
||||
// private static final long serialVersionUID = 3297717505428005316L;
|
||||
// public int age;
|
||||
// public String sex;
|
||||
// public A test;
|
||||
// }
|
||||
//
|
||||
// static class C implements Serializable {
|
||||
// private static final long serialVersionUID = 3297717505428005316L;
|
||||
// public int age;
|
||||
// public String sex;
|
||||
// public A test;
|
||||
// }
|
||||
//
|
||||
// private static void test1() {
|
||||
// ApiVO apiVO = new ApiVO(ApiVO.STATUS.S);
|
||||
// apiVO.setMsg("asdsadsad");
|
||||
// apiVO.setObject1("sadsadsad");
|
||||
//
|
||||
// Resps<String> obj = new Resps<>();
|
||||
// obj.setSucc("安达圣斗士", "ok");
|
||||
// System.out.println(obj);
|
||||
// apiVO.setObject2(obj);
|
||||
// ApiVO apiVO1 = depthClone(apiVO);
|
||||
// System.out.println(apiVO == apiVO1);
|
||||
// System.out.println(JsonUtil.obj2Json(apiVO1));
|
||||
// System.out.println(JsonUtil.obj2Json(apiVO1.getObject1(String.class)));
|
||||
// System.out.println(JsonUtil.obj2Json(apiVO1.getObject2(Resps.class)));
|
||||
// System.out.println(apiVO1.getObject2(Resps.class) == obj);
|
||||
// }
|
||||
}
|
||||
|
@ -25,4 +25,25 @@ public @interface ToUeProperty {
|
||||
* @return
|
||||
*/
|
||||
boolean ignore() default false;
|
||||
|
||||
/**
|
||||
* 转换格式
|
||||
* <p>目前只支持</p>
|
||||
* <p> {@link java.util.Date}、{@link java.time.LocalDate}、{@link java.time.LocalDateTime}
|
||||
* 转{@link Integer}(10位长度)、{@link Long}(13位长度)</p>
|
||||
* 其余无效
|
||||
*
|
||||
* @return <pre>
|
||||
* this in out <br/>
|
||||
* {@link java.util.Date} {@link Integer} {@link Integer}10位长度时间戳<br/>
|
||||
* {@link java.util.Date} {@link Long} {@link Long}13位长度时间戳<br/>
|
||||
* {@link java.time.LocalDate} {@link Integer} {@link Integer}10位长度时间戳<br/>
|
||||
* {@link java.time.LocalDate} {@link Long} {@link Long}13位长度时间戳(后三位为000)<br/>
|
||||
* {@link java.time.LocalDateTime} {@link Integer} {@link Integer}10位长度时间戳<br/>
|
||||
* {@link java.time.LocalDateTime} {@link Long} {@link Long}13位长度时间戳
|
||||
*
|
||||
* </pre>
|
||||
*/
|
||||
Class<?> type() default ObjUtil.class;
|
||||
|
||||
}
|
154
src/test/java/com/yexuejc/base/util/ObjUtilTest.java
Normal file
154
src/test/java/com/yexuejc/base/util/ObjUtilTest.java
Normal file
@ -0,0 +1,154 @@
|
||||
package com.yexuejc.base.util;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.math.BigDecimal;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* json化输出,改变属性名称,输出类型 {@link ObjUtil} + {@link ToUeProperty} 测试类
|
||||
*
|
||||
* @author: yexuejc
|
||||
* @date: 2021-02-03 10:02:55
|
||||
*/
|
||||
class ObjUtilTest {
|
||||
|
||||
public static void main(String[] args) {
|
||||
start();
|
||||
}
|
||||
|
||||
public static void start(){
|
||||
B a = new B();
|
||||
a.nameFirst = "张三";
|
||||
a.ageInt = "5165458";
|
||||
a.testAss = "asdasdsad";
|
||||
a.setaM1("method1");
|
||||
a.setbM1("b1Mthod1");
|
||||
a.protectedStr = "protectedStr";
|
||||
a.amount=new BigDecimal("3");
|
||||
a.time = LocalDateTime.now();
|
||||
a.dateTime=new Date();
|
||||
C c = new C();
|
||||
c.ageInt = "test";
|
||||
a.c = c;
|
||||
a.list = new ArrayList<>();
|
||||
a.list.add(c);
|
||||
Map<String, Object> underlineMap = ObjUtil.getUnderlineMap(a, false, false);
|
||||
System.out.println(JsonUtil.formatPrinter(underlineMap));
|
||||
}
|
||||
|
||||
static class A implements Serializable {
|
||||
private static final long serialVersionUID = -8462118058721865488L;
|
||||
public String nameFirst;
|
||||
public String ageInt;
|
||||
private String aM1;
|
||||
@ToUeProperty("p_str")
|
||||
protected String protectedStr;
|
||||
|
||||
public String getaM1() {
|
||||
return aM1;
|
||||
}
|
||||
|
||||
public A setaM1(String aM1) {
|
||||
this.aM1 = aM1;
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
static class B extends A {
|
||||
private static final long serialVersionUID = -8462118058721865488L;
|
||||
public String testAss;
|
||||
private String bM1;
|
||||
|
||||
@ToUeProperty(type = Integer.class)
|
||||
public BigDecimal amount;
|
||||
|
||||
@ToUeProperty(type = String.class)
|
||||
public LocalDateTime time;
|
||||
|
||||
public Date dateTime;
|
||||
|
||||
private C c;
|
||||
List<C> list;
|
||||
|
||||
public String getbM1() {
|
||||
return bM1;
|
||||
}
|
||||
|
||||
public B setbM1(String bM1) {
|
||||
this.bM1 = bM1;
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
static class C extends A {
|
||||
private static final long serialVersionUID = -8462118058721865488L;
|
||||
public String testAss;
|
||||
private String bM1;
|
||||
|
||||
public String getbM1() {
|
||||
return bM1;
|
||||
}
|
||||
|
||||
public C setbM1(String bM1) {
|
||||
this.bM1 = bM1;
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
// public static void main(String[] args) {
|
||||
//// test1();
|
||||
//// test2();
|
||||
//
|
||||
// }
|
||||
//
|
||||
// private static void test2() {
|
||||
// B t = new B();
|
||||
// t.sex = "男";
|
||||
// t.age = 18;
|
||||
// A test = new A();
|
||||
// test.name = "张三";
|
||||
// t.test = test;
|
||||
// B b = depthClone(t);
|
||||
// System.out.println(JsonUtil.obj2Json(b));
|
||||
// }
|
||||
//
|
||||
// static class A implements Serializable {
|
||||
// private static final long serialVersionUID = -8462118058721865488L;
|
||||
// public String name;
|
||||
// }
|
||||
//
|
||||
// static class B implements Serializable {
|
||||
// private static final long serialVersionUID = 3297717505428005316L;
|
||||
// public int age;
|
||||
// public String sex;
|
||||
// public A test;
|
||||
// }
|
||||
//
|
||||
// static class C implements Serializable {
|
||||
// private static final long serialVersionUID = 3297717505428005316L;
|
||||
// public int age;
|
||||
// public String sex;
|
||||
// public A test;
|
||||
// }
|
||||
//
|
||||
// private static void test1() {
|
||||
// ApiVO apiVO = new ApiVO(ApiVO.STATUS.S);
|
||||
// apiVO.setMsg("asdsadsad");
|
||||
// apiVO.setObject1("sadsadsad");
|
||||
//
|
||||
// Resps<String> obj = new Resps<>();
|
||||
// obj.setSucc("安达圣斗士", "ok");
|
||||
// System.out.println(obj);
|
||||
// apiVO.setObject2(obj);
|
||||
// ApiVO apiVO1 = depthClone(apiVO);
|
||||
// System.out.println(apiVO == apiVO1);
|
||||
// System.out.println(JsonUtil.obj2Json(apiVO1));
|
||||
// System.out.println(JsonUtil.obj2Json(apiVO1.getObject1(String.class)));
|
||||
// System.out.println(JsonUtil.obj2Json(apiVO1.getObject2(Resps.class)));
|
||||
// System.out.println(apiVO1.getObject2(Resps.class) == obj);
|
||||
// }
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user