mirror of
https://gitee.com/jzsw-it/yexuejc-base.git
synced 2025-06-07 22:34:04 +08:00
first commit
This commit is contained in:
commit
278bfb3a29
103
pom.xml
Normal file
103
pom.xml
Normal file
@ -0,0 +1,103 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>com.yexuejc.base</groupId>
|
||||
<artifactId>yexuejc-base</artifactId>
|
||||
<version>1.0.0</version>
|
||||
|
||||
<properties>
|
||||
<repos.yexuejc.ivt.url>http://47.100.13.178:7081/nexus/content/groups/public</repos.yexuejc.ivt.url>
|
||||
<repos.yexuejc.prod.url>http://47.100.13.178:7081/nexus/content/groups/public</repos.yexuejc.prod.url>
|
||||
<repos.yexuejc.dist.url>http://47.100.13.178:7081/nexus/content/repositories</repos.yexuejc.dist.url>
|
||||
|
||||
<repos.aliyun.url>http://maven.aliyun.com/nexus/content/groups/public</repos.aliyun.url>
|
||||
<repos.jitpack.url>https://jitpack.io</repos.jitpack.url>
|
||||
<jjwt.version>0.7.0</jjwt.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<!-- JJWT -->
|
||||
<dependency>
|
||||
<groupId>io.jsonwebtoken</groupId>
|
||||
<artifactId>jjwt</artifactId>
|
||||
<version>${jjwt.version}</version>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
|
||||
<build>
|
||||
<finalName>${project.artifactId}</finalName>
|
||||
<pluginManagement>
|
||||
<plugins>
|
||||
<!-- 打包源码 -->
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-source-plugin</artifactId>
|
||||
<version>3.0.1</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>attach-sources</id>
|
||||
<goals>
|
||||
<goal>jar-no-fork</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
<!-- 使用spring boot的maven插件进行打包 -->
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
<executions>
|
||||
<execution>
|
||||
<goals>
|
||||
<goal>build-info</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
<configuration>
|
||||
<!-- 是否打出可执行的jar包(仅支持Linux格式) -->
|
||||
<executable>true</executable>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</pluginManagement>
|
||||
</build>
|
||||
<repositories>
|
||||
<repository>
|
||||
<id>yexuejc-prod-nexus-public</id>
|
||||
<name>yexuejc-prod-nexus-public</name>
|
||||
<url>${repos.yexuejc.prod.url}</url>
|
||||
</repository>
|
||||
<repository>
|
||||
<id>yexuejc-ivt-nexus-public</id>
|
||||
<name>yexuejc-ivt-nexus-public</name>
|
||||
<url>${repos.yexuejc.ivt.url}</url>
|
||||
</repository>
|
||||
<repository>
|
||||
<id>aliyun-nexus-public</id>
|
||||
<name>aliyun-nexus-public</name>
|
||||
<url>${repos.aliyun.url}</url>
|
||||
</repository>
|
||||
<repository>
|
||||
<id>jitpack.io</id>
|
||||
<url>${repos.jitpack.url}</url>
|
||||
</repository>
|
||||
</repositories>
|
||||
<!-- 中间件jar包发布目标 -->
|
||||
<distributionManagement>
|
||||
<repository>
|
||||
<id>releases</id>
|
||||
<name>nexus-release</name>
|
||||
<url>${repos.yexuejc.dist.url}/releases</url>
|
||||
</repository>
|
||||
<snapshotRepository>
|
||||
<id>snapshots</id>
|
||||
<name>nexus-snapshots</name>
|
||||
<url>${repos.yexuejc.dist.url}/snapshots</url>
|
||||
</snapshotRepository>
|
||||
</distributionManagement>
|
||||
</project>
|
62
src/main/java/com/yexuejc/base/constant/RespsConsts.java
Normal file
62
src/main/java/com/yexuejc/base/constant/RespsConsts.java
Normal file
@ -0,0 +1,62 @@
|
||||
package com.yexuejc.base.constant;
|
||||
|
||||
/**
|
||||
* 网络请求统一返回 常量
|
||||
*
|
||||
* @PackageName: com.yexuejc.util.base
|
||||
* @Description:
|
||||
* @author: maxf
|
||||
* @date: 2017/12/27 16:47
|
||||
*/
|
||||
public class RespsConsts {
|
||||
|
||||
private RespsConsts() {
|
||||
}
|
||||
|
||||
/**
|
||||
* HTTP请求Header字段,用户登录认证凭证
|
||||
*/
|
||||
public static String HEADER_AUTHORIZATION = "Authorization";
|
||||
/**
|
||||
* HTTP请求Header字段,用户未登录时返回认证要求
|
||||
*/
|
||||
public static String HEADER_WWW_AUTHENTICATE = "www-authenticate";
|
||||
/**
|
||||
* HTTP请求Header字段,客户端信息
|
||||
*/
|
||||
public static String HEADER_X_USER_AGENT = "X-User-Agent";
|
||||
/**
|
||||
* HTTP请求Header字段,Content-Type
|
||||
*/
|
||||
public static String HEADER_CONTENT_TYPE = "Content-Type";
|
||||
|
||||
/**
|
||||
* www-authenticate字段可选值,jwt认证
|
||||
*/
|
||||
public static String WWW_AUTHENTICATE_JWT = "jwt";
|
||||
|
||||
/**
|
||||
* 成功
|
||||
*/
|
||||
public static final String CODE_SUCCESS = "S";
|
||||
public static final String MSG_SUCCESS_HTTP = "请求成功";
|
||||
public static final String MSG_SUCCESS_OPERATE = "操作成功";
|
||||
/**
|
||||
* 失败
|
||||
*/
|
||||
public static final String CODE_FAIL = "F";
|
||||
public static final String MSG_FAIL_HTTP = "请求失败";
|
||||
public static final String MSG_FAIL_OPERATE = "操作失败";
|
||||
/**
|
||||
* 错误
|
||||
*/
|
||||
public static final String CODE_ERROR = "E";
|
||||
public static final String MSG_ERROT_HTTP = "请求错误";
|
||||
public static final String MSG_ERROT_OPERATE = "操作错误";
|
||||
|
||||
/**
|
||||
* 参数校验错误
|
||||
*/
|
||||
public static final String CODE_VALIDATION = "V";
|
||||
public static final String MSG_VALIDATION = "参数错误";
|
||||
}
|
140
src/main/java/com/yexuejc/base/http/Resps.java
Normal file
140
src/main/java/com/yexuejc/base/http/Resps.java
Normal file
@ -0,0 +1,140 @@
|
||||
package com.yexuejc.base.http;
|
||||
|
||||
import com.yexuejc.base.constant.RespsConsts;
|
||||
import com.yexuejc.base.util.JsonUtil;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* 网络请求统一返回类
|
||||
*
|
||||
* @PackageName: com.yexuejc.util.base
|
||||
* @Description: 网络请求统一返回类
|
||||
* @author: maxf
|
||||
* @date: 2017/12/27 16:33
|
||||
*/
|
||||
public class Resps<T> implements Serializable {
|
||||
|
||||
|
||||
/**
|
||||
* 状态
|
||||
*/
|
||||
private String code;
|
||||
/**
|
||||
* 内容
|
||||
*/
|
||||
private T data;
|
||||
/**
|
||||
* 消息
|
||||
*/
|
||||
private String[] msg;
|
||||
|
||||
public Resps() {
|
||||
}
|
||||
|
||||
/**
|
||||
* 多个消息
|
||||
*
|
||||
* @param code
|
||||
* @param msg
|
||||
*/
|
||||
public Resps(String code, String[] msg) {
|
||||
this.code = code;
|
||||
this.msg = msg;
|
||||
}
|
||||
|
||||
/**
|
||||
* 单个消息
|
||||
*
|
||||
* @param code
|
||||
* @param msg
|
||||
*/
|
||||
public Resps(String code, String msg) {
|
||||
this.code = code;
|
||||
this.msg = new String[]{msg};
|
||||
}
|
||||
|
||||
public Resps<T> setSucc(T t) {
|
||||
setSucc(t, RespsConsts.MSG_SUCCESS_OPERATE);
|
||||
return this;
|
||||
}
|
||||
|
||||
public Resps<T> setSucc(T t, String msg) {
|
||||
setSucc(t, new String[]{msg});
|
||||
return this;
|
||||
}
|
||||
|
||||
public Resps<T> setSucc(T t, String[] msg) {
|
||||
this.setData(t);
|
||||
this.setCode(RespsConsts.CODE_SUCCESS);
|
||||
this.setMsg(msg);
|
||||
return this;
|
||||
}
|
||||
|
||||
public Resps setErr(String code, String[] msg) {
|
||||
this.setCode(code);
|
||||
this.setMsg(msg);
|
||||
return this;
|
||||
}
|
||||
|
||||
public static Resps success(String msg) {
|
||||
return new Resps(RespsConsts.CODE_SUCCESS, msg);
|
||||
}
|
||||
|
||||
public static Resps success() {
|
||||
return new Resps(RespsConsts.CODE_SUCCESS, RespsConsts.MSG_SUCCESS_OPERATE);
|
||||
}
|
||||
|
||||
public static Resps error(String msg) {
|
||||
return new Resps(RespsConsts.CODE_ERROR, msg);
|
||||
}
|
||||
|
||||
public static Resps fail(String msg) {
|
||||
return new Resps(RespsConsts.CODE_FAIL, msg);
|
||||
}
|
||||
|
||||
public static Resps error(String code, String msg) {
|
||||
return new Resps(code, msg);
|
||||
}
|
||||
|
||||
public static Resps error(String code, String[] msg) {
|
||||
return new Resps(code, msg);
|
||||
}
|
||||
|
||||
public static Resps fail(String code, String msg) {
|
||||
return new Resps(code, msg);
|
||||
}
|
||||
|
||||
public static Resps fail(String code, String[] msg) {
|
||||
return new Resps(code, msg);
|
||||
}
|
||||
|
||||
public String getCode() {
|
||||
return code;
|
||||
}
|
||||
|
||||
public void setCode(String code) {
|
||||
this.code = code;
|
||||
}
|
||||
|
||||
public T getData() {
|
||||
return data;
|
||||
}
|
||||
|
||||
public void setData(T data) {
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
public String[] getMsg() {
|
||||
return msg;
|
||||
}
|
||||
|
||||
public void setMsg(String[] msg) {
|
||||
this.msg = msg;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return JsonUtil.obj2Json(this);
|
||||
}
|
||||
}
|
149
src/main/java/com/yexuejc/base/pojo/ApiVO.java
Normal file
149
src/main/java/com/yexuejc/base/pojo/ApiVO.java
Normal file
@ -0,0 +1,149 @@
|
||||
package com.yexuejc.base.pojo;
|
||||
|
||||
|
||||
import com.yexuejc.base.util.JsonUtil;
|
||||
import com.yexuejc.base.util.StrUtil;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* 接口交互APi
|
||||
*
|
||||
* @PackageName: com.mcworle.ecentm.api.user.web.vo
|
||||
* @Description:
|
||||
* @author: maxf
|
||||
* @date: 2018/1/17 11:36
|
||||
*/
|
||||
public class ApiVO implements Serializable {
|
||||
public ApiVO() {
|
||||
}
|
||||
|
||||
public ApiVO(STATUS status, String code, String msg) {
|
||||
this.status = status;
|
||||
this.code = code;
|
||||
this.msgs = StrUtil.isNotEmpty(msg) ? new String[]{msg} : null;
|
||||
}
|
||||
|
||||
public enum STATUS {
|
||||
/**
|
||||
* 成功
|
||||
*/
|
||||
S,
|
||||
/**
|
||||
* 失败
|
||||
*/
|
||||
F,
|
||||
/**
|
||||
* 错误
|
||||
*/
|
||||
E
|
||||
}
|
||||
|
||||
/**
|
||||
* 状态
|
||||
*/
|
||||
private STATUS status = STATUS.S;
|
||||
/**
|
||||
* code
|
||||
*/
|
||||
private String code;
|
||||
/**
|
||||
* 消息
|
||||
*/
|
||||
private String[] msgs;
|
||||
|
||||
|
||||
/**
|
||||
* 值1
|
||||
*/
|
||||
private Object object1;
|
||||
/**
|
||||
* 值2
|
||||
*/
|
||||
private Object object2;
|
||||
|
||||
public <T extends Object> void setObject1(T obj) {
|
||||
object1 = obj;
|
||||
}
|
||||
|
||||
public <T extends Object> T getObject1(Class<T> clazz) {
|
||||
return (T) object1;
|
||||
}
|
||||
|
||||
public <T extends Object> void setObject2(T obj) {
|
||||
object2 = obj;
|
||||
}
|
||||
|
||||
public <T extends Object> T getObject2(Class<T> clazz) {
|
||||
return (T) object2;
|
||||
}
|
||||
|
||||
public ApiVO setStatus(STATUS status, String code, String msg) {
|
||||
this.status = status;
|
||||
this.code = code;
|
||||
this.msgs = StrUtil.isNotEmpty(msg) ? new String[]{msg} : null;
|
||||
return this;
|
||||
}
|
||||
|
||||
public ApiVO setStatus(STATUS status, String code, String[] msg) {
|
||||
this.status = status;
|
||||
this.code = code;
|
||||
this.msgs = msg;
|
||||
return this;
|
||||
}
|
||||
|
||||
public boolean isSucc() {
|
||||
if (STATUS.S.name().equals(status.name())) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean isErr() {
|
||||
if (STATUS.E.name().equals(status.name())) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public void setMsg(String msg) {
|
||||
this.msgs = StrUtil.isNotEmpty(msg) ? new String[]{msg} : null;
|
||||
}
|
||||
|
||||
public boolean isFail() {
|
||||
return !isSucc();
|
||||
}
|
||||
|
||||
public String getCode() {
|
||||
return code;
|
||||
}
|
||||
|
||||
public void setCode(String code) {
|
||||
this.code = code;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return JsonUtil.obj2Json(this);
|
||||
}
|
||||
|
||||
public STATUS getStatus() {
|
||||
return status;
|
||||
}
|
||||
|
||||
public void setStatus(STATUS status) {
|
||||
this.status = status;
|
||||
}
|
||||
|
||||
public void setMsgs(String[] msgs) {
|
||||
this.msgs = msgs;
|
||||
}
|
||||
|
||||
public String[] getMsgs() {
|
||||
return msgs;
|
||||
}
|
||||
|
||||
public String getMsg() {
|
||||
return msgs == null ? "" : msgs[0];
|
||||
}
|
||||
}
|
33
src/main/java/com/yexuejc/base/pojo/BaseVO.java
Normal file
33
src/main/java/com/yexuejc/base/pojo/BaseVO.java
Normal file
@ -0,0 +1,33 @@
|
||||
package com.yexuejc.base.pojo;
|
||||
|
||||
import com.yexuejc.base.util.JsonUtil;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* @PackageName: com.yexuejc.util.base.pojo
|
||||
* @Description:
|
||||
* @author: maxf
|
||||
* @date: 2018/1/17 13:58
|
||||
*/
|
||||
public class BaseVO implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = -1442656950873492155L;
|
||||
|
||||
public static interface Add {
|
||||
}
|
||||
|
||||
public static interface Del {
|
||||
}
|
||||
|
||||
public static interface Mdfy {
|
||||
}
|
||||
|
||||
public static interface Srch {
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return JsonUtil.obj2Json(this);
|
||||
}
|
||||
}
|
110
src/main/java/com/yexuejc/base/util/AlgorithmUtils.java
Normal file
110
src/main/java/com/yexuejc/base/util/AlgorithmUtils.java
Normal file
@ -0,0 +1,110 @@
|
||||
package com.yexuejc.base.util;
|
||||
|
||||
|
||||
/**
|
||||
* 算法工具类
|
||||
*
|
||||
* @ClassName: AlgorithmUtils
|
||||
* @Description:算法工具类
|
||||
* @author: maxf
|
||||
* @date: 2017年11月23日 下午3:17:58
|
||||
*/
|
||||
public class AlgorithmUtils {
|
||||
private static final int LENGTH_1 = 1;
|
||||
private static final int LENGTH_2 = 2;
|
||||
private static final int LENGTH_3 = 3;
|
||||
private static final int LENGTH_36 = 36;
|
||||
|
||||
private AlgorithmUtils() {
|
||||
}
|
||||
|
||||
private static final String X36 = "0123456789abcdefghijklmnopqrstuvwxyz";
|
||||
@SuppressWarnings("unused")
|
||||
private static final String X62 = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
||||
|
||||
/**
|
||||
* 生成下一个3位数36进制code
|
||||
*
|
||||
* @param subNum 当前code序数
|
||||
* @return
|
||||
* @throws NextCodeException String 下个序数的code
|
||||
* @Title: getNextCode
|
||||
* @Description: 生成下一个3位数36进制code
|
||||
* @throw
|
||||
*/
|
||||
public static String getNextCode(int subNum) throws NextCodeException {
|
||||
String nextCode = x10ConvertTo36(subNum);
|
||||
if (nextCode.length() < LENGTH_1) {
|
||||
nextCode = "000";
|
||||
} else if (nextCode.length() == LENGTH_1) {
|
||||
nextCode = "00" + nextCode;
|
||||
} else if (nextCode.length() == LENGTH_2) {
|
||||
nextCode = "0" + nextCode;
|
||||
} else if (nextCode.length() > LENGTH_3) {
|
||||
throw new NextCodeException();
|
||||
}
|
||||
return nextCode;
|
||||
}
|
||||
|
||||
/**
|
||||
* 10进制转换成36进制
|
||||
*
|
||||
* @param val 10进制
|
||||
* @return String 36进制
|
||||
* @Title: x10ConvertTo36
|
||||
* @Description: 10进制转换成36进制
|
||||
* @throw
|
||||
*/
|
||||
public static String x10ConvertTo36(int val) {
|
||||
String result = "";
|
||||
while (val >= LENGTH_36) {
|
||||
result = X36.toCharArray()[val % 36] + result;
|
||||
val /= 36;
|
||||
}
|
||||
if (val >= 0) {
|
||||
result = X36.toCharArray()[val] + result;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 36进制转10进制
|
||||
*
|
||||
* @param pStr 36进制
|
||||
* @return int 10进制
|
||||
* @Title: x36ConvertTo10
|
||||
* @Description: 36进制转10进制
|
||||
* @throw
|
||||
*/
|
||||
public static int x36ConvertTo10(String pStr) {
|
||||
if (pStr == "") {
|
||||
return 0;
|
||||
}
|
||||
// 目标十进制数初始化为0
|
||||
int deciaml = 0;
|
||||
// 记录次方,初始为36进制长度 -1
|
||||
int power = pStr.length() - 1;
|
||||
// 将36进制字符串转换成char[]
|
||||
char[] keys = pStr.toCharArray();
|
||||
for (int i = 0; i < pStr.length(); i++) {
|
||||
// 拿到36进制对应的10进制数
|
||||
int value = X36.indexOf(keys[i]);
|
||||
deciaml = (int) (deciaml + value * Math.pow(X36.length(), power));
|
||||
// 执行完毕 次方自减
|
||||
power--;
|
||||
}
|
||||
return deciaml;
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成下一个3位数36进制code 异常
|
||||
*
|
||||
* @ClassName: NextCodeException
|
||||
* @Description: 生成下一个3位数36进制code 出现异常时抛出
|
||||
* @author: maxf
|
||||
* @date: 2017年11月22日 下午4:38:40
|
||||
*/
|
||||
static class NextCodeException extends Exception {
|
||||
private static final long serialVersionUID = 8956943499144648985L;
|
||||
}
|
||||
}
|
202
src/main/java/com/yexuejc/base/util/DateTimeUtil.java
Normal file
202
src/main/java/com/yexuejc/base/util/DateTimeUtil.java
Normal file
@ -0,0 +1,202 @@
|
||||
package com.yexuejc.base.util;
|
||||
|
||||
import java.time.*;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.time.temporal.TemporalAdjuster;
|
||||
import java.time.temporal.TemporalAdjusters;
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* 新版时间日期出来 工具
|
||||
*
|
||||
* @author: maxf
|
||||
* @date: 2018/3/27 10:44
|
||||
*/
|
||||
public class DateTimeUtil {
|
||||
/**
|
||||
* 获取本年第一天
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public static LocalDate getYear4First() {
|
||||
return getYear4First(LocalDate.now());
|
||||
}
|
||||
|
||||
/**
|
||||
* 指定日期 年第一天
|
||||
*
|
||||
* @param now
|
||||
* @return
|
||||
*/
|
||||
public static LocalDate getYear4First(LocalDate now) {
|
||||
return now.with(TemporalAdjusters.firstDayOfYear());
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取本年最后一天
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public static LocalDate getYear4Last() {
|
||||
return getYear4Last(LocalDate.now());
|
||||
}
|
||||
|
||||
/**
|
||||
* 指定日期 年最后一天
|
||||
*
|
||||
* @param now
|
||||
* @return
|
||||
*/
|
||||
public static LocalDate getYear4Last(LocalDate now) {
|
||||
return now.with(TemporalAdjusters.lastDayOfYear());
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 获取本月第一天
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public static LocalDate getMonth4First() {
|
||||
return getMonth4First(LocalDate.now());
|
||||
}
|
||||
|
||||
/**
|
||||
* 指定日期 月第一天
|
||||
*
|
||||
* @param now
|
||||
* @return
|
||||
*/
|
||||
public static LocalDate getMonth4First(LocalDate now) {
|
||||
return now.with(TemporalAdjusters.firstDayOfMonth());
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取本月第最后一天
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public static LocalDate getMonth4Last() {
|
||||
return getMonth4Last(LocalDate.now());
|
||||
}
|
||||
|
||||
/**
|
||||
* 指定日期 月第最后一天
|
||||
*
|
||||
* @param now
|
||||
* @return
|
||||
*/
|
||||
public static LocalDate getMonth4Last(LocalDate now) {
|
||||
return now.with(TemporalAdjusters.lastDayOfMonth());
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取本周第一天
|
||||
*
|
||||
* @return {@link LocalDate}
|
||||
*/
|
||||
public static LocalDate getWeek4First() {
|
||||
return getWeek4First(LocalDate.now());
|
||||
}
|
||||
|
||||
/**
|
||||
* 指定日期 周第一天
|
||||
*
|
||||
* @param date
|
||||
* @return
|
||||
*/
|
||||
public static LocalDate getWeek4First(LocalDate date) {
|
||||
TemporalAdjuster FIRST_OF_WEEK = TemporalAdjusters.ofDateAdjuster(localDate -> localDate.minusDays(localDate.getDayOfWeek().getValue() - DayOfWeek.MONDAY.getValue()));
|
||||
return date.with(FIRST_OF_WEEK);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取本周最后一天
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public static LocalDate getWeek4Last() {
|
||||
return getWeek4Last(LocalDate.now());
|
||||
}
|
||||
|
||||
/**
|
||||
* 指定日期 周最后一天
|
||||
*
|
||||
* @param date
|
||||
* @return
|
||||
*/
|
||||
public static LocalDate getWeek4Last(LocalDate date) {
|
||||
TemporalAdjuster LAST_OF_WEEK = TemporalAdjusters.ofDateAdjuster(localDate -> localDate.plusDays(DayOfWeek.SUNDAY.getValue() - localDate.getDayOfWeek().getValue()));
|
||||
return date.with(LAST_OF_WEEK);
|
||||
}
|
||||
|
||||
/**
|
||||
* ZonedDateTime 转 Date
|
||||
*
|
||||
* @param zonedDateTime
|
||||
* @return
|
||||
*/
|
||||
public static Date zonedDateTime2Date(ZonedDateTime zonedDateTime) {
|
||||
ZoneId zoneId = ZoneId.systemDefault();
|
||||
ZonedDateTime zdt = zonedDateTime.withZoneSameInstant(zoneId);
|
||||
Date date = Date.from(zdt.toInstant());
|
||||
return date;
|
||||
}
|
||||
|
||||
/**
|
||||
* Date 转 ZonedDateTime
|
||||
*
|
||||
* @param date
|
||||
* @return
|
||||
*/
|
||||
public static ZonedDateTime date2ZonedDateTime(Date date) {
|
||||
Instant instant = date.toInstant();
|
||||
ZoneId zoneId = ZoneId.systemDefault();
|
||||
return instant.atZone(zoneId).withZoneSameInstant(zoneId);
|
||||
}
|
||||
|
||||
/**
|
||||
* 格式化时间 <br/>
|
||||
* 格式 yyyy-MM-dd HH:mm:ss
|
||||
*
|
||||
* @param dateTime
|
||||
* @return
|
||||
*/
|
||||
public static String format(LocalDateTime dateTime) {
|
||||
return format(dateTime, "");
|
||||
}
|
||||
|
||||
/**
|
||||
* 格式化时间
|
||||
*
|
||||
* @param dateTime
|
||||
* @param pattern 格式 默认:yyyy-MM-dd HH:mm:ss
|
||||
* @return
|
||||
*/
|
||||
public static String format(LocalDateTime dateTime, String pattern) {
|
||||
if (pattern.isEmpty()) {
|
||||
pattern = "yyyy-MM-dd HH:mm:ss";
|
||||
}
|
||||
DateTimeFormatter df = DateTimeFormatter.ofPattern(pattern);
|
||||
return df.format(dateTime);
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
// SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
||||
// System.out.println(df.format(zonedDateTime2Date(ZonedDateTime.now())));
|
||||
// System.out.println(df2.format(date2ZonedDateTime(new Date())));
|
||||
|
||||
// System.out.println(getWeek4First());
|
||||
System.out.println(format(getWeek4First(LocalDate.parse("2018-02-10")).atTime(LocalTime.MIN)));
|
||||
System.out.println(format(getWeek4Last(LocalDate.parse("2018-02-10")).atTime(LocalTime.MAX)));
|
||||
|
||||
// System.out.println(format(getMonth4First().atTime(LocalTime.MIN)));
|
||||
// System.out.println(format(getMonth4Last().atTime(LocalTime.MAX)));
|
||||
|
||||
// System.out.println(format(getYear4First().atTime(LocalTime.MIN)));
|
||||
// System.out.println(format(getYear4Last().atTime(LocalTime.MAX)));
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
158
src/main/java/com/yexuejc/base/util/DateUtil.java
Normal file
158
src/main/java/com/yexuejc/base/util/DateUtil.java
Normal file
@ -0,0 +1,158 @@
|
||||
package com.yexuejc.base.util;
|
||||
|
||||
import java.text.DateFormat;
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
import java.util.Locale;
|
||||
|
||||
public class DateUtil {
|
||||
private 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 String DAY_START_TIME = "00:00:00";
|
||||
public static String DAY_END_TIME = "23:59:59";
|
||||
|
||||
/**
|
||||
* 取得服务器当前日期
|
||||
*
|
||||
* @return 当前日期:格式(yyyy-MM-dd)
|
||||
*/
|
||||
static public String currentDate() {
|
||||
Date curDate = new Date();
|
||||
return DATE_FORMAT.format(curDate);
|
||||
}
|
||||
|
||||
/**
|
||||
* 取得服务器当前时间
|
||||
*
|
||||
* @return 当前日期:格式(hh:mm:ss)
|
||||
*/
|
||||
static public String currentTime() {
|
||||
Date curDate = new Date();
|
||||
return TIME_FORMAT.format(curDate);
|
||||
}
|
||||
|
||||
/**
|
||||
* 取得服务器当前日期和时间
|
||||
*
|
||||
* @return 当前日期:格式(yyyy-MM-dd hh:mm:ss)
|
||||
*/
|
||||
static public String currentDateTime() {
|
||||
Date curDate = new Date();
|
||||
return DATE_TIME_FORMAT.format(curDate);
|
||||
}
|
||||
|
||||
/**
|
||||
* 比较两个日期大小
|
||||
*
|
||||
* @param date1
|
||||
* @param date2
|
||||
* @return date1>date2返回1;date1=date2返回0;date1<date2返回-1
|
||||
* @throws ParseException
|
||||
*/
|
||||
public static byte dateCompare(String date1, String date2) throws ParseException {
|
||||
|
||||
Date d1 = DATE_FORMAT.parse(date1);
|
||||
Date d2 = DATE_FORMAT.parse(date2);
|
||||
if (d1.before(d2)) {
|
||||
return -1;
|
||||
} else if (d1.equals(d2)) {
|
||||
return 0;
|
||||
} else {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 日期字符串转date
|
||||
*
|
||||
* @param date
|
||||
* @return Date
|
||||
* @throws ParseException
|
||||
*/
|
||||
public static Date str2date(String dateStr) throws ParseException {
|
||||
Date date = DATE_FORMAT.parse(dateStr);
|
||||
return date;
|
||||
}
|
||||
|
||||
/**
|
||||
* 日期字符串转date
|
||||
*
|
||||
* @param date
|
||||
* @return Date
|
||||
* @throws ParseException
|
||||
*/
|
||||
public static String date2str(Date date) throws ParseException {
|
||||
if (date != null) {
|
||||
return DATE_FORMAT.format(date);
|
||||
} else {
|
||||
return "null";
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取本周的日期
|
||||
*
|
||||
* @param dayOfWeek :可用值:Calendar.SUNDAY,Calendar.MONDAY,
|
||||
* Calendar.TUESDAY,Calendar.WEDNESDAY,Calendar.THURSDAY,
|
||||
* Calendar.FRIDAY,Calendar.SATURDAY
|
||||
* @return
|
||||
*/
|
||||
public static String getCurrentWeek(int dayOfWeek) {
|
||||
Calendar calendar = Calendar.getInstance(Locale.CHINA);
|
||||
if (Calendar.SUNDAY == dayOfWeek) {
|
||||
calendar.add(Calendar.WEEK_OF_MONTH, 1);
|
||||
}
|
||||
calendar.set(Calendar.DAY_OF_WEEK, dayOfWeek);
|
||||
return DATE_FORMAT.format(calendar.getTime());
|
||||
}
|
||||
|
||||
/**
|
||||
* 两个日期相减
|
||||
*
|
||||
* @param date1
|
||||
* @param date2
|
||||
* @param flag : 'y':得出相差年数,'M':相差月数,'d':相差天数,'h':相差小时数
|
||||
* 'm':相差分钟数,'s':相差秒数,其他:相差毫秒数
|
||||
* @return
|
||||
*/
|
||||
public static long dateMinus(Date date1, Date date2, char flag) {
|
||||
long msMinus = date1.getTime() - date2.getTime();
|
||||
switch (flag) {
|
||||
case 'y':
|
||||
return msMinus / (365L * 24L * 60L * 60L * 1000L);
|
||||
case 'M':
|
||||
return msMinus / (30L * 24L * 60L * 60L * 1000L);
|
||||
case 'd':
|
||||
return msMinus / (24L * 60L * 60L * 1000L);
|
||||
case 'h':
|
||||
return msMinus / (60L * 60L * 1000L);
|
||||
case 'm':
|
||||
return msMinus / (60L * 1000L);
|
||||
case 's':
|
||||
return msMinus / 1000L;
|
||||
default:
|
||||
return msMinus;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 一个日期加上xx天,返回加法之后的日期
|
||||
*
|
||||
* @param date
|
||||
* @param days
|
||||
* @return
|
||||
*/
|
||||
public static Date datePlus(Date date, int days) {
|
||||
Calendar calendar = Calendar.getInstance();
|
||||
calendar.setTime(date);
|
||||
calendar.set(Calendar.DAY_OF_MONTH, calendar.get(Calendar.DAY_OF_MONTH) + days);
|
||||
return calendar.getTime();
|
||||
}
|
||||
|
||||
}
|
49
src/main/java/com/yexuejc/base/util/ExcelImportUtils.java
Normal file
49
src/main/java/com/yexuejc/base/util/ExcelImportUtils.java
Normal file
@ -0,0 +1,49 @@
|
||||
package com.yexuejc.base.util;
|
||||
|
||||
/**
|
||||
* excel 格式验证工具
|
||||
* @ClassName: ExcelImportUtils
|
||||
* @Description:
|
||||
* @author: maxf
|
||||
* @date: 2017/12/27 16:42
|
||||
*/
|
||||
public class ExcelImportUtils {
|
||||
|
||||
|
||||
/**
|
||||
* 是否是2003的excel,返回true是2003
|
||||
*
|
||||
* @param filePath
|
||||
* @return
|
||||
* @Description:是否是2003的excel,返回true是2003
|
||||
*/
|
||||
public static boolean isExcel2003(String filePath) {
|
||||
return filePath.matches("^.+\\.(?i)(xls)$");
|
||||
}
|
||||
|
||||
/**
|
||||
* 是否是2007的excel,返回true是2007
|
||||
*
|
||||
* @param filePath
|
||||
* @return
|
||||
* @Description:是否是2007的excel,返回true是2007
|
||||
*/
|
||||
public static boolean isExcel2007(String filePath) {
|
||||
return filePath.matches("^.+\\.(?i)(xlsx)$");
|
||||
}
|
||||
|
||||
/**
|
||||
* 验证EXCEL文件
|
||||
*
|
||||
* @param filePath
|
||||
* @return
|
||||
*/
|
||||
public static boolean validateExcel(String filePath) {
|
||||
if (filePath == null || !(isExcel2003(filePath) || isExcel2007(filePath))) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
}
|
68
src/main/java/com/yexuejc/base/util/FileUtil.java
Normal file
68
src/main/java/com/yexuejc/base/util/FileUtil.java
Normal file
@ -0,0 +1,68 @@
|
||||
package com.yexuejc.base.util;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* @author maxf:yexue
|
||||
* @className FileUtil
|
||||
* @description 工具类
|
||||
* @time 2017年11月3日 下午3:12:49
|
||||
*/
|
||||
public class FileUtil {
|
||||
private FileUtil() {
|
||||
}
|
||||
|
||||
private static final String TYPE_TAR_GZ = ".tar.gz";
|
||||
private static final String TAR_GZ = "tar.gz";
|
||||
|
||||
public static String getFileType(String fileName) {
|
||||
if (fileName.lastIndexOf(TYPE_TAR_GZ) > 0) {
|
||||
return TAR_GZ;
|
||||
}
|
||||
return fileName.substring(fileName.lastIndexOf(".") + 1);
|
||||
}
|
||||
|
||||
// 判断文件是否存在
|
||||
public static void judeFileExists(File file) {
|
||||
|
||||
if (file.exists()) {
|
||||
System.out.println("file exists");
|
||||
} else {
|
||||
System.out.println("file not exists, create it ...");
|
||||
try {
|
||||
file.createNewFile();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* <pre>
|
||||
* 判断文件夹是否存在, 不存在创建【采用mkdirs】
|
||||
* 相关解释:
|
||||
* 1、File类的createNewFile根据抽象路径创建一个新的空文件,当抽象路径制定的文件存在时,创建失败
|
||||
* 2、File类的mkdir方法根据抽象路径创建目录
|
||||
* 3、File类的mkdirs方法根据抽象路径创建目录,包括创建必需但不存在的父目录
|
||||
* 4、File类的createTempFile方法创建临时文件,可以制定临时文件的文件名前缀、后缀及文件所在的目录,如果不指定目录,则存放在系统的临时文件夹下。
|
||||
* 5、除mkdirs方法外,以上方法在创建文件和目录时,必须保证目标文件不存在,而且父目录存在,否则会创建失败
|
||||
* </pre>
|
||||
*/
|
||||
public static void judeDirExists(File file) {
|
||||
|
||||
if (file.exists()) {
|
||||
if (file.isDirectory()) {
|
||||
System.out.println("dir exists");
|
||||
} else {
|
||||
System.out.println("the same name file exists, can not create dir");
|
||||
}
|
||||
} else {
|
||||
System.out.println("dir not exists, create it ...");
|
||||
file.mkdirs();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
435
src/main/java/com/yexuejc/base/util/IdcardValidator.java
Normal file
435
src/main/java/com/yexuejc/base/util/IdcardValidator.java
Normal file
@ -0,0 +1,435 @@
|
||||
package com.yexuejc.base.util;
|
||||
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* 校验身份证
|
||||
*
|
||||
* @author yexue
|
||||
* @expl <pre>
|
||||
* 15位身份证号码:第7、8位为出生年份(两位数),第9、10位为出生月份,第11、12位代表出生日期,第15位代表性别,奇数为男,偶数为女。
|
||||
* 18位身份证号码:第7、8、9、10位为出生年份(四位数),第11、第12位为出生月份,第13、14位代表出生日期,第17位代表性别,奇数为男,偶数为女。
|
||||
* 最后一位为校验位
|
||||
* </pre>
|
||||
* @time 2017年11月13日 下午5:27:09
|
||||
*/
|
||||
public class IdcardValidator {
|
||||
private static final int LENGTH_15 = 15;
|
||||
private static final int LENGTH_18 = 18;
|
||||
private static final int LENGTH_2 = 2;
|
||||
private static final String SEX_NAN = "男";
|
||||
|
||||
private IdcardValidator() {
|
||||
}
|
||||
|
||||
/**
|
||||
* <pre>
|
||||
*
|
||||
* 省、直辖市代码表:
|
||||
* 11 : 北京 12 : 天津 13 : 河北 14 : 山西 15 : 内蒙古
|
||||
* 21 : 辽宁 22 : 吉林 23 : 黑龙江 31 : 上海 32 : 江苏
|
||||
* 33 : 浙江 34 : 安徽 35 : 福建 36 : 江西 37 : 山东
|
||||
* 41 : 河南 42 : 湖北 43 : 湖南 44 : 广东 45 : 广西 46 : 海南
|
||||
* 50 : 重庆 51 : 四川 52 : 贵州 53 : 云南 54 : 西藏
|
||||
* 61 : 陕西 62 : 甘肃 63 : 青海 64 : 宁夏 65 : 新疆
|
||||
* 71 : 台湾
|
||||
* 81 : 香港 82 : 澳门
|
||||
* 91 : 国外
|
||||
* </pre>
|
||||
*/
|
||||
private static String[] cityCode = {"11", "12", "13", "14", "15", "21", "22", "23", "31", "32", "33", "34", "35",
|
||||
"36", "37", "41", "42", "43", "44", "45", "46", "50", "51", "52", "53", "54", "61", "62", "63", "64", "65",
|
||||
"71", "81", "82", "91"};
|
||||
|
||||
/**
|
||||
* 每位加权因子
|
||||
*/
|
||||
private static int power[] = {7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2};
|
||||
|
||||
/**
|
||||
* 验证所有的身份证的合法性
|
||||
*
|
||||
* @param identity 身份证号
|
||||
* @param sex 性别
|
||||
* @return boolean
|
||||
* @Title: isValidatedAllIdcard
|
||||
* @Description: 验证所有的身份证的合法性
|
||||
* @throw
|
||||
*/
|
||||
public static boolean isValidatedAllIdcard(String identity, String sex) {
|
||||
boolean b = isValidatedAllIdcard(identity);
|
||||
if (b && identity.length() == LENGTH_18) {
|
||||
b = false;
|
||||
int char17 = Integer.parseInt(identity.substring(16, 17));
|
||||
int i = 0;
|
||||
if (SEX_NAN.equals(sex)) {
|
||||
i = 1;
|
||||
}
|
||||
if ((char17 % LENGTH_2) == i) {
|
||||
b = true;
|
||||
}
|
||||
}
|
||||
return b;
|
||||
}
|
||||
|
||||
/**
|
||||
* 验证所有的身份证的合法性
|
||||
*
|
||||
* @param idcard 身份证
|
||||
* @return 合法返回true,否则返回false
|
||||
*/
|
||||
public static boolean isValidatedAllIdcard(String idcard) {
|
||||
if (idcard == null || "".equals(idcard)) {
|
||||
return false;
|
||||
}
|
||||
if (idcard.length() == LENGTH_15) {
|
||||
return validate15IDCard(idcard);
|
||||
}
|
||||
return validate18Idcard(idcard);
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* 判断18位身份证的合法性
|
||||
* </p>
|
||||
* 根据〖中华人民共和国国家标准GB11643-1999〗中有关公民身份号码的规定,公民身份号码是特征组合码,由十七位数字本体码和一位数字校验码组成。
|
||||
* 排列顺序从左至右依次为:六位数字地址码,八位数字出生日期码,三位数字顺序码和一位数字校验码。
|
||||
* <p>
|
||||
* 顺序码: 表示在同一地址码所标识的区域范围内,对同年、同月、同 日出生的人编定的顺序号,顺序码的奇数分配给男性,偶数分配 给女性。
|
||||
* </p>
|
||||
* <p>
|
||||
* 1.前1、2位数字表示:所在省份的代码; 2.第3、4位数字表示:所在城市的代码; 3.第5、6位数字表示:所在区县的代码;
|
||||
* 4.第7~14位数字表示:出生年、月、日; 5.第15、16位数字表示:所在地的派出所的代码; 6.第17位数字表示性别:奇数表示男性,偶数表示女性;
|
||||
* 7.第18位数字是校检码:也有的说是个人信息码,一般是随计算机的随机产生,用来检验身份证的正确性。校检码可以是0~9的数字,有时也用x表示。
|
||||
* </p>
|
||||
* <p>
|
||||
* 第十八位数字(校验码)的计算方法为: 1.将前面的身份证号码17位数分别乘以不同的系数。从第一位到第十七位的系数分别为:7 9 10 5 8 4 2 1
|
||||
* 6 3 7 9 10 5 8 4 2
|
||||
* </p>
|
||||
* <p>
|
||||
* 2.将这17位数字和系数相乘的结果相加。
|
||||
* </p>
|
||||
* <p>
|
||||
* 3.用加出来和除以11,看余数是多少
|
||||
* </p>
|
||||
* 4.余数只可能有0 1 2 3 4 5 6 7 8 9 10这11个数字。其分别对应的最后一位身份证的号码为1 0 X 9 8 7 6 5 4 3 2。
|
||||
* <p>
|
||||
* 5.通过上面得知如果余数是2,就会在身份证的第18位数字上出现罗马数字的Ⅹ。如果余数是10,身份证的最后一位号码就是2。
|
||||
* </p>
|
||||
*
|
||||
* @param idcard
|
||||
* @return
|
||||
*/
|
||||
private static boolean validate18Idcard(String idcard) {
|
||||
if (idcard == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// 非18位为假
|
||||
if (idcard.length() != LENGTH_18) {
|
||||
return false;
|
||||
}
|
||||
// 获取前17位
|
||||
String idcard17 = idcard.substring(0, 17);
|
||||
|
||||
// 前17位全部为数字
|
||||
if (!isDigital(idcard17)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
String provinceid = idcard.substring(0, 2);
|
||||
// 校验省份
|
||||
if (!checkProvinceid(provinceid)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// 校验出生日期
|
||||
String birthday = idcard.substring(6, 14);
|
||||
|
||||
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
|
||||
|
||||
try {
|
||||
Date birthDate = sdf.parse(birthday);
|
||||
String tmpDate = sdf.format(birthDate);
|
||||
if (!tmpDate.equals(birthday)) {
|
||||
// 出生年月日不正确
|
||||
return false;
|
||||
}
|
||||
|
||||
} catch (ParseException e1) {
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
// 获取第18位
|
||||
String idcard18Code = idcard.substring(17, 18);
|
||||
|
||||
char c[] = idcard17.toCharArray();
|
||||
|
||||
int bit[] = converCharToInt(c);
|
||||
|
||||
int sum17 = 0;
|
||||
|
||||
sum17 = getPowerSum(bit);
|
||||
|
||||
// 将和值与11取模得到余数进行校验码判断
|
||||
String checkCode = getCheckCodeBySum(sum17);
|
||||
if (null == checkCode) {
|
||||
return false;
|
||||
}
|
||||
// 将身份证的第18位与算出来的校码进行匹配,不相等就为假
|
||||
if (!idcard18Code.equalsIgnoreCase(checkCode)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 校验15位身份证
|
||||
* <p>
|
||||
* <pre>
|
||||
*
|
||||
* 只校验省份和出生年月日
|
||||
* </pre>
|
||||
*
|
||||
* @param idcard
|
||||
* @return
|
||||
*/
|
||||
private static boolean validate15IDCard(String idcard) {
|
||||
if (idcard == null) {
|
||||
return false;
|
||||
}
|
||||
// 非15位为假
|
||||
if (idcard.length() != LENGTH_15) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// 15全部为数字
|
||||
if (!isDigital(idcard)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
String provinceid = idcard.substring(0, 2);
|
||||
// 校验省份
|
||||
if (!checkProvinceid(provinceid)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
String birthday = idcard.substring(6, 12);
|
||||
|
||||
SimpleDateFormat sdf = new SimpleDateFormat("yyMMdd");
|
||||
|
||||
try {
|
||||
Date birthDate = sdf.parse(birthday);
|
||||
String tmpDate = sdf.format(birthDate);
|
||||
if (!tmpDate.equals(birthday)) {
|
||||
// 身份证日期错误
|
||||
return false;
|
||||
}
|
||||
|
||||
} catch (ParseException e1) {
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 将15位的身份证转成18位身份证
|
||||
*
|
||||
* @param idcard
|
||||
* @return
|
||||
*/
|
||||
@SuppressWarnings("unused")
|
||||
private static String convertIdcarBy15bit(String idcard) {
|
||||
if (idcard == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// 非15位身份证
|
||||
if (idcard.length() != LENGTH_15) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// 15全部为数字
|
||||
if (!isDigital(idcard)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
String provinceid = idcard.substring(0, 2);
|
||||
// 校验省份
|
||||
if (!checkProvinceid(provinceid)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
String birthday = idcard.substring(6, 12);
|
||||
|
||||
SimpleDateFormat sdf = new SimpleDateFormat("yyMMdd");
|
||||
|
||||
Date birthdate = null;
|
||||
try {
|
||||
birthdate = sdf.parse(birthday);
|
||||
String tmpDate = sdf.format(birthdate);
|
||||
if (!tmpDate.equals(birthday)) {
|
||||
// 身份证日期错误
|
||||
return null;
|
||||
}
|
||||
|
||||
} catch (ParseException e1) {
|
||||
return null;
|
||||
}
|
||||
|
||||
Calendar cday = Calendar.getInstance();
|
||||
cday.setTime(birthdate);
|
||||
String year = String.valueOf(cday.get(Calendar.YEAR));
|
||||
|
||||
String idcard17 = idcard.substring(0, 6) + year + idcard.substring(8);
|
||||
|
||||
char c[] = idcard17.toCharArray();
|
||||
String checkCode = "";
|
||||
|
||||
// 将字符数组转为整型数组
|
||||
int bit[] = converCharToInt(c);
|
||||
|
||||
int sum17 = 0;
|
||||
sum17 = getPowerSum(bit);
|
||||
|
||||
// 获取和值与11取模得到余数进行校验码
|
||||
checkCode = getCheckCodeBySum(sum17);
|
||||
|
||||
// 获取不到校验位
|
||||
if (null == checkCode) {
|
||||
return null;
|
||||
}
|
||||
// 将前17位与第18位校验码拼接
|
||||
idcard17 += checkCode;
|
||||
return idcard17;
|
||||
}
|
||||
|
||||
/**
|
||||
* 校验省份
|
||||
*
|
||||
* @param provinceid
|
||||
* @return 合法返回TRUE,否则返回FALSE
|
||||
*/
|
||||
private static boolean checkProvinceid(String provinceid) {
|
||||
for (String id : cityCode) {
|
||||
if (id.equals(provinceid)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 数字验证
|
||||
*
|
||||
* @param str
|
||||
* @return
|
||||
*/
|
||||
private static boolean isDigital(String str) {
|
||||
return str.matches("^[0-9]*$");
|
||||
}
|
||||
|
||||
/**
|
||||
* 将身份证的每位和对应位的加权因子相乘之后,再得到和值
|
||||
*
|
||||
* @param bit
|
||||
* @return
|
||||
*/
|
||||
private static int getPowerSum(int[] bit) {
|
||||
|
||||
int sum = 0;
|
||||
|
||||
if (power.length != bit.length) {
|
||||
return sum;
|
||||
}
|
||||
|
||||
for (int i = 0; i < bit.length; i++) {
|
||||
for (int j = 0; j < power.length; j++) {
|
||||
if (i == j) {
|
||||
sum = sum + bit[i] * power[j];
|
||||
}
|
||||
}
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
|
||||
/**
|
||||
* 将和值与11取模得到余数进行校验码判断
|
||||
*
|
||||
* @param sum17
|
||||
* @return 校验位
|
||||
*/
|
||||
private static String getCheckCodeBySum(int sum17) {
|
||||
String checkCode = null;
|
||||
switch (sum17 % 11) {
|
||||
case 10:
|
||||
checkCode = "2";
|
||||
break;
|
||||
case 9:
|
||||
checkCode = "3";
|
||||
break;
|
||||
case 8:
|
||||
checkCode = "4";
|
||||
break;
|
||||
case 7:
|
||||
checkCode = "5";
|
||||
break;
|
||||
case 6:
|
||||
checkCode = "6";
|
||||
break;
|
||||
case 5:
|
||||
checkCode = "7";
|
||||
break;
|
||||
case 4:
|
||||
checkCode = "8";
|
||||
break;
|
||||
case 3:
|
||||
checkCode = "9";
|
||||
break;
|
||||
case 2:
|
||||
checkCode = "x";
|
||||
break;
|
||||
case 1:
|
||||
checkCode = "0";
|
||||
break;
|
||||
case 0:
|
||||
checkCode = "1";
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
return checkCode;
|
||||
}
|
||||
|
||||
/**
|
||||
* 将字符数组转为整型数组
|
||||
*
|
||||
* @param c
|
||||
* @return
|
||||
* @throws NumberFormatException
|
||||
*/
|
||||
private static int[] converCharToInt(char[] c) throws NumberFormatException {
|
||||
int[] a = new int[c.length];
|
||||
int k = 0;
|
||||
for (char temp : c) {
|
||||
a[k++] = Integer.parseInt(String.valueOf(temp));
|
||||
}
|
||||
return a;
|
||||
}
|
||||
|
||||
/*
|
||||
* public static void main(String[] args) throws Exception { String idcard15 =
|
||||
* "130321860311519"; String idcard18 = "513002199410010816";//
|
||||
* System.out.println(idcard18.substring(16,17)); // 15位身份证
|
||||
* System.out.println(isValidatedAllIdcard(idcard15)); // 18位身份证
|
||||
* System.out.println(isValidatedAllIdcard(idcard18)); // 15位身份证转18位身份证
|
||||
* System.out.println(convertIdcarBy15bit(idcard15)); }
|
||||
*/
|
||||
}
|
146
src/main/java/com/yexuejc/base/util/JsonUtil.java
Normal file
146
src/main/java/com/yexuejc/base/util/JsonUtil.java
Normal file
@ -0,0 +1,146 @@
|
||||
package com.yexuejc.base.util;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonInclude.Include;
|
||||
import com.fasterxml.jackson.core.JsonParseException;
|
||||
import com.fasterxml.jackson.core.JsonParser;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.*;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
public class JsonUtil {
|
||||
private JsonUtil() {
|
||||
}
|
||||
|
||||
/**
|
||||
* 作为单例全局使用
|
||||
*/
|
||||
private static ObjectMapper objectMapper = new ObjectMapper();
|
||||
|
||||
static {
|
||||
JsonUtil.initDefaultObjectMapper(JsonUtil.objectMapper);
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化ObjectMapper为默认属性
|
||||
*
|
||||
* @param objectMapper
|
||||
*/
|
||||
private static void initDefaultObjectMapper(ObjectMapper objectMapper) {
|
||||
objectMapper.setSerializationInclusion(Include.NON_NULL);
|
||||
objectMapper.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);
|
||||
objectMapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
|
||||
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
|
||||
objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
|
||||
objectMapper.setDateFormat(DateUtil.DATE_TIME_FORMAT);
|
||||
}
|
||||
|
||||
/**
|
||||
* 每调用一次生成一个全新的ObjectMapper供特殊场景使用,与通用ObjectMapper没有关系
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public static ObjectMapper genObjectMapper() {
|
||||
ObjectMapper objectMapper = new ObjectMapper();
|
||||
JsonUtil.initDefaultObjectMapper(objectMapper);
|
||||
return objectMapper;
|
||||
}
|
||||
|
||||
/**
|
||||
* 将json转换为某个类
|
||||
*
|
||||
* @param json InputStream类型json数据
|
||||
* @param cls 转换类class
|
||||
* @return 对象实例
|
||||
*/
|
||||
public static <T> T json2Obj(InputStream json, Class<T> cls) {
|
||||
T pojo = null;
|
||||
|
||||
try {
|
||||
pojo = objectMapper.readValue(json, cls);
|
||||
} catch (JsonParseException e) {
|
||||
} catch (JsonMappingException e) {
|
||||
} catch (IOException e) {
|
||||
}
|
||||
|
||||
return pojo;
|
||||
}
|
||||
|
||||
/**
|
||||
* 将json转换为某个类
|
||||
*
|
||||
* @param json String类型json数据
|
||||
* @param cls 转换类class
|
||||
* @return 对象实例
|
||||
*/
|
||||
public static <T> T json2Obj(String json, Class<T> cls) {
|
||||
T pojo = null;
|
||||
|
||||
try {
|
||||
pojo = objectMapper.readValue(json, cls);
|
||||
} catch (JsonParseException e) {
|
||||
} catch (JsonMappingException e) {
|
||||
} catch (IOException e) {
|
||||
}
|
||||
|
||||
return pojo;
|
||||
}
|
||||
|
||||
/**
|
||||
* Json字符串转换为Java对象
|
||||
*
|
||||
* @param json
|
||||
* @param parametrized
|
||||
* @param parameterClasses
|
||||
* @return
|
||||
*/
|
||||
public static <T> T json2Obj(String json, Class<T> parametrized, Class<?>... parameterClasses) {
|
||||
T pojo = null;
|
||||
JavaType javaType = objectMapper.getTypeFactory().constructParametrizedType(parametrized, parametrized,
|
||||
parameterClasses);
|
||||
try {
|
||||
pojo = objectMapper.readValue(json, javaType);
|
||||
} catch (JsonParseException e) {
|
||||
} catch (JsonMappingException e) {
|
||||
} catch (IOException e) {
|
||||
}
|
||||
return pojo;
|
||||
}
|
||||
|
||||
/**
|
||||
* Json字符串转换为Java对象
|
||||
*
|
||||
* @param json
|
||||
* @param parametrized
|
||||
* @param parameterClasses
|
||||
* @return
|
||||
*/
|
||||
public static <T> T json2Obj(InputStream json, Class<T> parametrized, Class<?>... parameterClasses) {
|
||||
T pojo = null;
|
||||
JavaType javaType = objectMapper.getTypeFactory().constructParametrizedType(parametrized, parametrized,
|
||||
parameterClasses);
|
||||
try {
|
||||
pojo = objectMapper.readValue(json, javaType);
|
||||
} catch (JsonParseException e) {
|
||||
} catch (JsonMappingException e) {
|
||||
} catch (IOException e) {
|
||||
}
|
||||
return pojo;
|
||||
}
|
||||
|
||||
/**
|
||||
* 将任何对象转换为json
|
||||
*
|
||||
* @param pojo 要转换的对象
|
||||
* @return 返回json
|
||||
*/
|
||||
public static String obj2Json(Object pojo) {
|
||||
String json = null;
|
||||
try {
|
||||
json = objectMapper.writeValueAsString(pojo);
|
||||
} catch (JsonProcessingException e) {
|
||||
}
|
||||
return json;
|
||||
}
|
||||
}
|
79
src/main/java/com/yexuejc/base/util/JwtUtil.java
Normal file
79
src/main/java/com/yexuejc/base/util/JwtUtil.java
Normal file
@ -0,0 +1,79 @@
|
||||
package com.yexuejc.base.util;
|
||||
|
||||
import io.jsonwebtoken.Jwts;
|
||||
import io.jsonwebtoken.SignatureAlgorithm;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.Map;
|
||||
|
||||
public class JwtUtil {
|
||||
/** 加密用KEY */
|
||||
private static String JWT_SIGNATURE_KEY = "h%OG8Y3WgA5AN7&6Ke7I#C1XvneW0N8a";
|
||||
/** token类型 */
|
||||
private static String JWT_HEADER_TYP = "JWT";
|
||||
/** token发行商 */
|
||||
private static String JWT_CLAIMS_ISS = "uselaw.com";
|
||||
|
||||
/**
|
||||
* 加密内容生成token
|
||||
*
|
||||
* @param subjectObj
|
||||
* @return
|
||||
*/
|
||||
public static String compact(Object subjectObj) {
|
||||
String subject = null;
|
||||
if (subjectObj instanceof String) {
|
||||
subject = (String) subjectObj;
|
||||
} else {
|
||||
subject = JsonUtil.obj2Json(subjectObj);
|
||||
}
|
||||
Date now = new Date();
|
||||
String token = Jwts.builder()
|
||||
// 设置token的唯一标识ID(claims.jti)
|
||||
.setId(StrUtil.genUUID())
|
||||
// 设置token类型(header.typ)
|
||||
.setHeaderParam("typ", JWT_HEADER_TYP)
|
||||
// 设置token发行时间为当前时间(claims.iat)
|
||||
.setIssuedAt(now)
|
||||
// 设置token发行商/发行者(claims.iss)
|
||||
.setIssuer(JWT_CLAIMS_ISS)
|
||||
// 设置token用户定义主体(claims.sub)
|
||||
.setSubject(subject)
|
||||
// 设置签名算法和KEY(signature)
|
||||
.signWith(SignatureAlgorithm.HS512, JWT_SIGNATURE_KEY)
|
||||
// 生成token
|
||||
.compact();
|
||||
return token;
|
||||
}
|
||||
|
||||
/**
|
||||
* 解密token为一个Map
|
||||
*
|
||||
* @param token
|
||||
* @return
|
||||
*/
|
||||
public static Map<?, ?> parse(String token) {
|
||||
return parse(token, Map.class);
|
||||
}
|
||||
|
||||
/**
|
||||
* 解密token为一个指定对象
|
||||
*
|
||||
* @param token
|
||||
* @param cls
|
||||
* @return
|
||||
*/
|
||||
public static <T> T parse(String token, Class<T> cls) {
|
||||
String subject = null;
|
||||
try {
|
||||
subject = Jwts.parser().setSigningKey(JWT_SIGNATURE_KEY).parseClaimsJws(token).getBody().getSubject();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
if (subject == null) {
|
||||
return null;
|
||||
}
|
||||
return JsonUtil.json2Obj(subject, cls);
|
||||
}
|
||||
|
||||
}
|
116
src/main/java/com/yexuejc/base/util/MapRemoveNullUtil.java
Normal file
116
src/main/java/com/yexuejc/base/util/MapRemoveNullUtil.java
Normal file
@ -0,0 +1,116 @@
|
||||
package com.yexuejc.base.util;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
public class MapRemoveNullUtil {
|
||||
private MapRemoveNullUtil() {
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 移除map中空key或者value空值
|
||||
*
|
||||
* @param map
|
||||
*/
|
||||
public static Map removeNullEntry(Map map) {
|
||||
removeNullKey(map);
|
||||
removeNullValue(map);
|
||||
return map;
|
||||
}
|
||||
|
||||
/**
|
||||
* 移除map的空key
|
||||
*
|
||||
* @param map
|
||||
* @return
|
||||
*/
|
||||
public static Map removeNullKey(Map map) {
|
||||
Set set = map.keySet();
|
||||
for (Iterator iterator = set.iterator(); iterator.hasNext(); ) {
|
||||
Object obj = (Object) iterator.next();
|
||||
remove(obj, iterator);
|
||||
}
|
||||
return map;
|
||||
}
|
||||
|
||||
/**
|
||||
* 移除map中的value空值
|
||||
*
|
||||
* @param map
|
||||
* @return
|
||||
*/
|
||||
public static Map removeNullValue(Map map) {
|
||||
Set set = map.keySet();
|
||||
for (Iterator iterator = set.iterator(); iterator.hasNext(); ) {
|
||||
Object obj = (Object) iterator.next();
|
||||
Object value = (Object) map.get(obj);
|
||||
remove(value, iterator);
|
||||
}
|
||||
return map;
|
||||
}
|
||||
|
||||
/**
|
||||
* Iterator 是工作在一个独立的线程中,并且拥有一个 mutex 锁。
|
||||
* Iterator 被创建之后会建立一个指向原来对象的单链索引表,当原来的对象数量发生变化时,这个索引表的内容不会同步改变,
|
||||
* 所以当索引指针往后移动的时候就找不到要迭代的对象,所以按照 fail-fast 原则 Iterator 会马上抛出 java.util.ConcurrentModificationException 异常。
|
||||
* 所以 Iterator 在工作的时候是不允许被迭代的对象被改变的。
|
||||
* 但你可以使用 Iterator 本身的方法 remove() 来删除对象, Iterator.remove() 方法会在删除当前迭代对象的同时维护索引的一致性。
|
||||
*
|
||||
* @param obj
|
||||
* @param iterator
|
||||
*/
|
||||
private static void remove(Object obj, Iterator iterator) {
|
||||
if (obj instanceof String) {
|
||||
String str = (String) obj;
|
||||
if (StrUtil.isEmpty(str)) {
|
||||
iterator.remove();
|
||||
}
|
||||
} else if (obj instanceof Collection) {
|
||||
Collection col = (Collection) obj;
|
||||
if (col == null || col.isEmpty()) {
|
||||
iterator.remove();
|
||||
}
|
||||
|
||||
} else if (obj instanceof Map) {
|
||||
Map temp = (Map) obj;
|
||||
if (temp == null || temp.isEmpty()) {
|
||||
iterator.remove();
|
||||
}
|
||||
|
||||
} else if (obj instanceof Object[]) {
|
||||
Object[] array = (Object[]) obj;
|
||||
if (array == null || array.length <= 0) {
|
||||
iterator.remove();
|
||||
}
|
||||
} else {
|
||||
if (obj == null) {
|
||||
iterator.remove();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// public static void main(String[] args) {
|
||||
// Map map = new HashMap();
|
||||
// map.put(1, "第一个值是数字");
|
||||
// map.put("2", "第2个值是字符串");
|
||||
// map.put(new String[]{"1","2"},"第3个值是数组");
|
||||
// map.put(new ArrayList(), "第4个值是List");
|
||||
// map.put(new HashMap(), "Map 无值");
|
||||
// map.put("5", "第5个");
|
||||
// map.put("6",null);
|
||||
// map.put("7", "");
|
||||
// map.put("8", " ");
|
||||
// System.out.println(map);
|
||||
// MapRemoveNullUtil.removeNullKey(map);
|
||||
// System.out.println();
|
||||
// System.out.println(map);
|
||||
// MapRemoveNullUtil.removeNullValue(map);
|
||||
// System.out.println();
|
||||
// System.out.println(map);
|
||||
// }
|
||||
|
||||
}
|
55
src/main/java/com/yexuejc/base/util/MoneyUtils.java
Normal file
55
src/main/java/com/yexuejc/base/util/MoneyUtils.java
Normal file
@ -0,0 +1,55 @@
|
||||
package com.yexuejc.base.util;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.text.DecimalFormat;
|
||||
|
||||
/**
|
||||
* q钱相关工具类
|
||||
*
|
||||
* @ClassName: MoneyUtils
|
||||
* @Description:
|
||||
* @author: maxf
|
||||
* @date: 2017年12月1日 下午5:33:57
|
||||
*/
|
||||
public class MoneyUtils {
|
||||
private MoneyUtils() {
|
||||
}
|
||||
|
||||
/**
|
||||
* 分转元
|
||||
*
|
||||
* @param num
|
||||
* @return String
|
||||
* @Title: formatPrice
|
||||
* @Description:分转元
|
||||
* @throw
|
||||
*/
|
||||
public static String toYuan(Integer num) {
|
||||
if (num == null) {
|
||||
return "0.00";
|
||||
}
|
||||
DecimalFormat df = new DecimalFormat("0.00");
|
||||
BigDecimal bigDecimal = new BigDecimal(num).divide(new BigDecimal(100));
|
||||
String str = df.format(bigDecimal);
|
||||
return str;
|
||||
}
|
||||
|
||||
/**
|
||||
* 元转分
|
||||
*
|
||||
* @param num
|
||||
* @return int
|
||||
* @Title: formatPrice
|
||||
* @Description: 元转分
|
||||
* @throw
|
||||
*/
|
||||
public static int toFen(String num) {
|
||||
if (num == null) {
|
||||
return 0;
|
||||
}
|
||||
DecimalFormat df = new DecimalFormat("#0");
|
||||
BigDecimal bigDecimal = new BigDecimal(num).multiply(new BigDecimal(100));
|
||||
String str = df.format(bigDecimal);
|
||||
return Integer.parseInt(str);
|
||||
}
|
||||
}
|
49
src/main/java/com/yexuejc/base/util/RegexUtils.java
Normal file
49
src/main/java/com/yexuejc/base/util/RegexUtils.java
Normal file
@ -0,0 +1,49 @@
|
||||
package com.yexuejc.base.util;
|
||||
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/**
|
||||
* 正则验证
|
||||
*
|
||||
* @author yexue
|
||||
* @expl
|
||||
* @time 2017年11月9日 上午11:01:24
|
||||
*/
|
||||
public class RegexUtils {
|
||||
private RegexUtils() {
|
||||
}
|
||||
|
||||
/**
|
||||
* 正则规则:小写字母
|
||||
*/
|
||||
public static final String REGEX_LOWERCASE = "^$|^[a-z\\d_]+$";
|
||||
/**
|
||||
* 正则规则:中国人姓名
|
||||
* 例:张三、买买提·也罗伊
|
||||
*/
|
||||
public static final String CHINA_NAME = "^[\\u4e00-\\u9fa5]+(·[\\u4e00-\\u9fa5]+)*$";
|
||||
;
|
||||
/**
|
||||
* 短信验证码:4位数字
|
||||
*/
|
||||
public static final String REGEX_NUM4 = "^$|^\\d{4}$";
|
||||
/**
|
||||
* cvn2:3位数字
|
||||
*/
|
||||
public static final String REGEX_NUM3 = "^$|^\\d{3}$";
|
||||
|
||||
/**
|
||||
* 正则:入参验证
|
||||
*
|
||||
* @param parms
|
||||
* @param regex
|
||||
* @return
|
||||
*/
|
||||
public static boolean regex(String parms, String regex) {
|
||||
Pattern pattern = Pattern.compile(regex);
|
||||
Matcher matcher = pattern.matcher(parms);
|
||||
return matcher.matches();
|
||||
}
|
||||
|
||||
}
|
195
src/main/java/com/yexuejc/base/util/StrUtil.java
Normal file
195
src/main/java/com/yexuejc/base/util/StrUtil.java
Normal file
@ -0,0 +1,195 @@
|
||||
package com.yexuejc.base.util;
|
||||
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.security.MessageDigest;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import static java.util.regex.Pattern.compile;
|
||||
|
||||
/**
|
||||
* @PackageName: com.yexuejc.util.base.util
|
||||
* @Description:
|
||||
* @author: maxf
|
||||
* @date: 2017/12/28 17:34
|
||||
*/
|
||||
public final class StrUtil {
|
||||
public static char[] HEX_CHAR = new char[]{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd',
|
||||
'e', 'f'};
|
||||
|
||||
private StrUtil() {
|
||||
}
|
||||
|
||||
public static boolean isEmpty(Object obj) {
|
||||
if (obj instanceof String) {
|
||||
return obj == null || "".equals((String) obj);
|
||||
} else if (obj instanceof Object[]) {
|
||||
return obj == null || ((Object[]) ((Object[]) obj)).length == 0;
|
||||
} else if (obj instanceof Collection) {
|
||||
return obj == null || ((Collection) obj).size() == 0;
|
||||
} else {
|
||||
return obj == null;
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean isNotEmpty(Object obj) {
|
||||
return !isEmpty(obj);
|
||||
}
|
||||
|
||||
public static String genUUID() {
|
||||
return UUID.randomUUID().toString().replaceAll("-", "");
|
||||
}
|
||||
|
||||
public static String genNum() {
|
||||
int hashCode = UUID.randomUUID().toString().hashCode();
|
||||
StringBuffer num = new StringBuffer();
|
||||
if (hashCode < 0) {
|
||||
hashCode = 0 - hashCode;
|
||||
num.append("0");
|
||||
} else {
|
||||
num.append("1");
|
||||
}
|
||||
|
||||
return num.append(String.format("%010d", hashCode)).toString().substring(0, 8);
|
||||
}
|
||||
|
||||
public static Map<String, String> parseUrlencoded(String urlencoded) {
|
||||
if (isEmpty(urlencoded)) {
|
||||
return null;
|
||||
} else {
|
||||
String[] entrys = urlencoded.split("&");
|
||||
if (isEmpty(entrys)) {
|
||||
return null;
|
||||
} else {
|
||||
Map<String, String> map = new HashMap();
|
||||
String[] kv = null;
|
||||
String[] var4 = entrys;
|
||||
int var5 = entrys.length;
|
||||
|
||||
for (int var6 = 0; var6 < var5; ++var6) {
|
||||
String entry = var4[var6];
|
||||
if (!isEmpty(entry)) {
|
||||
kv = entry.split("=");
|
||||
if (!isEmpty(kv)) {
|
||||
if (kv.length > 1) {
|
||||
map.put(kv[0], kv[1]);
|
||||
} else {
|
||||
map.put(kv[0], null);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return map;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static String toHex(byte[] buf) {
|
||||
StringBuffer strbuf = new StringBuffer(buf.length * 2);
|
||||
|
||||
for (int i = 0; i < buf.length; ++i) {
|
||||
if ((buf[i] & 255) < 16) {
|
||||
strbuf.append("0");
|
||||
}
|
||||
|
||||
strbuf.append(Long.toString((long) (buf[i] & 255), 16));
|
||||
}
|
||||
|
||||
return strbuf.toString();
|
||||
}
|
||||
|
||||
public static String toMD5(String str) {
|
||||
if (str == null) {
|
||||
return null;
|
||||
} else {
|
||||
MessageDigest md = null;
|
||||
|
||||
try {
|
||||
md = MessageDigest.getInstance("MD5");
|
||||
} catch (NoSuchAlgorithmException var3) {
|
||||
var3.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
|
||||
md.update(str.getBytes());
|
||||
byte[] tmp = md.digest();
|
||||
return toHex(tmp);
|
||||
}
|
||||
}
|
||||
|
||||
public static String iso2utf(String str) {
|
||||
String utfStr = null;
|
||||
|
||||
try {
|
||||
utfStr = new String(str.getBytes("ISO-8859-1"), "utf-8");
|
||||
} catch (UnsupportedEncodingException var3) {
|
||||
var3.printStackTrace();
|
||||
}
|
||||
|
||||
return utfStr;
|
||||
}
|
||||
|
||||
public static boolean isNumeric(String str) {
|
||||
Pattern pattern = compile("[0-9]*");
|
||||
Matcher isNum = pattern.matcher(str);
|
||||
return isNum.matches();
|
||||
}
|
||||
|
||||
public static String codeId(String id) {
|
||||
if (id != null && id.length() == 32) {
|
||||
StringBuilder coded = new StringBuilder();
|
||||
|
||||
int i;
|
||||
for (i = 0; i < 13; ++i) {
|
||||
coded.append(HEX_CHAR[(int) (Math.random() * 15.0D) + 1]);
|
||||
}
|
||||
|
||||
coded.append(id.substring(0, 11));
|
||||
|
||||
for (i = 0; i < 7; ++i) {
|
||||
coded.append(HEX_CHAR[(int) (Math.random() * 15.0D) + 1]);
|
||||
}
|
||||
|
||||
coded.append(id.substring(11));
|
||||
|
||||
for (i = 0; i < 12; ++i) {
|
||||
coded.append(HEX_CHAR[(int) (Math.random() * 15.0D) + 1]);
|
||||
}
|
||||
|
||||
return coded.toString();
|
||||
} else {
|
||||
return id;
|
||||
}
|
||||
}
|
||||
|
||||
public static String decodeId(String coded) {
|
||||
if (coded != null && coded.length() == 64) {
|
||||
StringBuilder id = new StringBuilder();
|
||||
id.append(coded.substring(13, 24));
|
||||
id.append(coded.substring(31, 52));
|
||||
return id.toString();
|
||||
} else {
|
||||
return coded;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 替换手机号中间4位为*
|
||||
*
|
||||
* @param mobile
|
||||
* @return String
|
||||
* @Title: replaceMobile
|
||||
* @Description: 替换手机号中间4位为*
|
||||
* @throw
|
||||
*/
|
||||
public static String replaceMobile(String mobile) {
|
||||
return mobile.replaceAll("(\\d{3})\\d{4}(\\d{4})", "$1****$2");
|
||||
}
|
||||
}
|
25
src/main/java/com/yexuejc/base/util/SysUtils.java
Normal file
25
src/main/java/com/yexuejc/base/util/SysUtils.java
Normal file
@ -0,0 +1,25 @@
|
||||
package com.yexuejc.base.util;
|
||||
|
||||
/**
|
||||
* 系统工具类
|
||||
*
|
||||
* @PackageName: com.yexuejc.util.base
|
||||
* @Description:
|
||||
* @author: maxf
|
||||
* @date: 2017/12/28 16:12
|
||||
*/
|
||||
public class SysUtils {
|
||||
private static final String PROJECT_ROOT_PATH = "java.io.tmpdir";
|
||||
|
||||
private SysUtils() {
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取临时缓存路径
|
||||
*
|
||||
* @return String
|
||||
*/
|
||||
public static String getCachePath() {
|
||||
return System.getProperty(PROJECT_ROOT_PATH);
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user