mirror of
https://gitee.com/jzsw-it/yexuejc-base.git
synced 2025-11-18 20:57:43 +08:00
Compare commits
5 Commits
1.5.2-jre1
...
0403c7c693
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
0403c7c693 | ||
|
|
cf8231017d | ||
|
|
1415200b1a | ||
|
|
0954eb64b5 | ||
|
|
70aad08b97 |
10
UPDATE.md
10
UPDATE.md
@@ -1,6 +1,16 @@
|
||||
yexuejc-base 更新记录
|
||||
------------------
|
||||
|
||||
#### version :1.5.3-jre11
|
||||
**time: ** <br/>
|
||||
**branch:** jre11 <br/>
|
||||
**update:** <br/>
|
||||
1. [FileUtil.java](src/main/java/com/yexuejc/base/util/FileUtil.java)增加读取大文件自定义方法和单纯读取方法
|
||||
2. JsonUtil 增加objToMap;优化obj2Json
|
||||
3. DateUtil 标准化日期时间的转换函数
|
||||
4. [AES.java](src/main/java/com/yexuejc/base/encrypt/AES.java) 兼容ECB(虽然不再建议利用)
|
||||
---
|
||||
|
||||
#### version :1.5.2-jre11
|
||||
**time:2024-4-7 14:34:33** <br/>
|
||||
**branch:** jre11 <br/>
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
package com.yexuejc.base.converter;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonParser;
|
||||
import com.fasterxml.jackson.databind.DeserializationContext;
|
||||
import com.fasterxml.jackson.databind.deser.std.StdScalarDeserializer;
|
||||
|
||||
/**
|
||||
* 反序列化中内容为空时,返回Integer为空
|
||||
* <p>使用方式:@JsonDeserialize(using = IntegerNullValueDeserializer.class)</p>
|
||||
* @author: yexuejc
|
||||
* @date: 2024/4/15 18:08
|
||||
*/
|
||||
public class IntegerNullValueDeserializer extends StdScalarDeserializer<Integer> {
|
||||
public IntegerNullValueDeserializer() {
|
||||
super(Integer.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
|
||||
String value = p.getValueAsString();
|
||||
if (isInteger(value)) {
|
||||
return super._parseInteger(p, ctxt, Integer.class);
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private static boolean isInteger(String s) {
|
||||
try {
|
||||
Integer.parseInt(s);
|
||||
return true;
|
||||
} catch (NumberFormatException e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -93,7 +93,10 @@ public class AES {
|
||||
byte[] plaintext = new byte[plaintextLength];
|
||||
System.arraycopy(dataBytes, 0, plaintext, 0, dataBytes.length);
|
||||
SecretKeySpec keyspec = new SecretKeySpec(key.getBytes(charset), AES_ALGORITHM);
|
||||
IvParameterSpec ivspec = new IvParameterSpec(iv.getBytes(charset));
|
||||
IvParameterSpec ivspec = null;
|
||||
if(!algorithm.name.contains("ECB")){
|
||||
ivspec = new IvParameterSpec(iv.getBytes(charset));
|
||||
}
|
||||
cipher.init(Cipher.ENCRYPT_MODE, keyspec, ivspec);
|
||||
byte[] encrypted = cipher.doFinal(plaintext);
|
||||
return Base64.getEncoder().encodeToString(encrypted);
|
||||
@@ -115,7 +118,10 @@ public class AES {
|
||||
byte[] encrypted = Base64.getDecoder().decode(data);
|
||||
Cipher cipher = Cipher.getInstance(algorithm.name);
|
||||
SecretKeySpec keyspec = new SecretKeySpec(key.getBytes(charset), AES_ALGORITHM);
|
||||
IvParameterSpec ivspec = new IvParameterSpec(iv.getBytes(charset));
|
||||
IvParameterSpec ivspec = null;
|
||||
if(!algorithm.name.contains("ECB")){
|
||||
ivspec = new IvParameterSpec(iv.getBytes(charset));
|
||||
}
|
||||
cipher.init(Cipher.DECRYPT_MODE, keyspec, ivspec);
|
||||
byte[] original = cipher.doFinal(encrypted);
|
||||
return new String(original, charset).trim();
|
||||
|
||||
@@ -80,57 +80,122 @@ public class DateUtil {
|
||||
/**
|
||||
* 日期字符串转date
|
||||
*
|
||||
* @param dateStr
|
||||
* @return Date
|
||||
* @param dateStr 格式:yyyy-MM-dd
|
||||
* @return Date 日期
|
||||
* @throws ParseException
|
||||
* @deprecated 替代参考 {@link #parseDate(String, String)}
|
||||
* @see 1.5.2
|
||||
*/
|
||||
@Deprecated
|
||||
public static Date str2date(String dateStr) throws ParseException {
|
||||
Date date = DATE_FORMAT.parse(dateStr);
|
||||
return date;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* date转字符串
|
||||
*
|
||||
* @param date
|
||||
* @return Date
|
||||
* @throws ParseException
|
||||
* @param date 日期
|
||||
* @return String 格式 yyyy-MM-dd
|
||||
* @deprecated 替代参考 {@link #formatDate(Date, String)} 或 {@link #formatDate(Date, String, Locale)}
|
||||
* @see 1.5.2
|
||||
*/
|
||||
public static String date2str(Date date) throws ParseException {
|
||||
@Deprecated
|
||||
public static String date2str(Date date) {
|
||||
if (date != null) {
|
||||
return DATE_FORMAT.format(date);
|
||||
} else {
|
||||
return "null";
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 日期字符串转dateTime
|
||||
*
|
||||
* @param dateStr
|
||||
* @return
|
||||
* @param dateStr 格式:yyyy-MM-dd HH:mm:ss.SSS
|
||||
* @return Date 时间
|
||||
* @throws ParseException
|
||||
* @deprecated 替代参考 {@link #parseDate(String, String)}
|
||||
* @see 1.5.2
|
||||
*/
|
||||
@Deprecated
|
||||
public static Date str2dateTime(String dateStr) throws ParseException {
|
||||
Date date = DATE_TIME_FORMAT.parse(dateStr);
|
||||
return date;
|
||||
return DATE_TIME_FORMAT.parse(dateStr);
|
||||
}
|
||||
|
||||
/**
|
||||
* dateTime转字符串
|
||||
*
|
||||
* @param date
|
||||
* @return Date
|
||||
* @throws ParseException
|
||||
* @param date 时间
|
||||
* @return String 格式:yyyy-MM-dd HH:mm:ss.SSS
|
||||
* @deprecated 替代参考 {@link #formatDate(Date, String)} 或 {@link #formatDate(Date, String, Locale)}
|
||||
* @see 1.5.2
|
||||
*/
|
||||
public static String dateTime2str(Date date) throws ParseException {
|
||||
@Deprecated
|
||||
public static String dateTime2str(Date date) {
|
||||
if (date != null) {
|
||||
return DATE_TIME_FORMAT.format(date);
|
||||
} else {
|
||||
return "null";
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 格式化当前日期为指定格式的字符串。
|
||||
*
|
||||
* @param dateFormat 日期格式字符串,用于指定日期的输出格式,例如"yyyy-MM-dd HH:mm:ss"。
|
||||
* @return 格式化后的当前日期字符串。
|
||||
*/
|
||||
public static String formatDateNow(String dateFormat) {
|
||||
return formatDate(new Date(), dateFormat); // 使用系统当前时间生成日期对象,并按指定格式进行格式化。
|
||||
}
|
||||
|
||||
/**
|
||||
* 解析字符串形式的日期到Date对象。
|
||||
*
|
||||
* @param dateStr 待解析的日期字符串。
|
||||
* @param dateFormat 日期字符串的格式。
|
||||
* @return 返回解析后的Date对象,如果解析失败则返回null。
|
||||
* @throws ParseException
|
||||
*/
|
||||
public static Date parseDate(String dateStr, String dateFormat) throws ParseException {
|
||||
SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
|
||||
// 尝试根据给定的日期格式解析日期字符串
|
||||
return sdf.parse(dateStr);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据指定的日期格式和地域格式化日期对象为字符串。
|
||||
* 如果未指定地域,则默认使用中文地域格式。
|
||||
*
|
||||
* @param date 需要格式化的日期对象。
|
||||
* @param dateFormat 日期格式字符串,例如"yyyy-MM-dd"。
|
||||
* @return 格式化后的日期字符串。
|
||||
*/
|
||||
public static String formatDate(Date date, String dateFormat) {
|
||||
return formatDate(date, dateFormat, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据指定的日期格式、地域格式化日期对象为字符串。
|
||||
*
|
||||
* @param date 需要格式化的日期对象。
|
||||
* @param dateFormat 日期格式字符串,例如"yyyy-MM-dd"。
|
||||
* @param locale 地域设置,如果为null则默认使用中文地域。
|
||||
* @return 格式化后的日期字符串。
|
||||
*/
|
||||
public static String formatDate(Date date, String dateFormat, Locale locale) {
|
||||
SimpleDateFormat sdf;
|
||||
// 根据是否提供了地域参数来创建SimpleDateFormat实例
|
||||
if (StrUtil.isEmpty(locale)) {
|
||||
sdf = new SimpleDateFormat(dateFormat, Locale.CHINA);
|
||||
} else {
|
||||
sdf = new SimpleDateFormat(dateFormat, locale);
|
||||
}
|
||||
return sdf.format(date);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取本周的日期
|
||||
|
||||
@@ -18,6 +18,7 @@ import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Base64;
|
||||
import java.util.List;
|
||||
import java.util.function.Function;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
import java.util.zip.CRC32;
|
||||
@@ -38,11 +39,14 @@ import io.jsonwebtoken.lang.Assert;
|
||||
* @time 2017年11月3日 下午3:12:49
|
||||
*/
|
||||
public class FileUtil {
|
||||
static Logger logger = Logger.getLogger(FileUtil.class.getName());
|
||||
private static final Logger logger = Logger.getLogger(FileUtil.class.getName());
|
||||
|
||||
private FileUtil() {
|
||||
}
|
||||
|
||||
|
||||
private static final String NEW_LINE = "\n";
|
||||
private static final String CONSTANT_DOT = ".";
|
||||
private static final String TYPE_TAR_GZ = ".tar.gz";
|
||||
private static final String TYPE_CSV = ".csv";
|
||||
private static final String TAR_GZ = "tar.gz";
|
||||
@@ -61,7 +65,7 @@ public class FileUtil {
|
||||
if (fileName.lastIndexOf(TYPE_TAR_GZ) > 0) {
|
||||
return TAR_GZ;
|
||||
}
|
||||
return fileName.substring(fileName.lastIndexOf(".") + 1);
|
||||
return fileName.substring(fileName.lastIndexOf(CONSTANT_DOT) + 1);
|
||||
} catch (Exception e) {
|
||||
logger.severe("file doesn't exist or is not a file");
|
||||
}
|
||||
@@ -332,22 +336,19 @@ public class FileUtil {
|
||||
}
|
||||
|
||||
/**
|
||||
* 分段读取大文件
|
||||
* 分段读取大文件(不限格式)
|
||||
*
|
||||
* @param csvFilePath 文件路径
|
||||
* @param filePath 文件路径
|
||||
* @param readFileBean 分段每次读取的bean 初始值需要设置每次读取的行数
|
||||
* @param <T> 读取结果类型bean
|
||||
* @return
|
||||
* @return 文件分页读取内容(自定义处理后)及读取信息
|
||||
*/
|
||||
public static <T> ReadFileBean<T> readBigFile(String csvFilePath, ReadFileBean<T> readFileBean, Class<T> readCls) throws IOException {
|
||||
if (!isFileExist(csvFilePath)) {
|
||||
throw new FileNotFoundException(String.format("解析用的csv: [%s] 文件不存在。", csvFilePath));
|
||||
}
|
||||
if (!csvFilePath.endsWith(TYPE_CSV)) {
|
||||
throw new IOException(String.format("解析用的csv: [%s] 文件不是CSV文件格式。", csvFilePath));
|
||||
public static <T> ReadFileBean<T> readBigFile(String filePath, ReadFileBean<T> readFileBean, Function<List<String>, List<T>> readAfter) throws IOException {
|
||||
if (!isFileExist(filePath)) {
|
||||
throw new FileNotFoundException(String.format("[%s]文件不存在。", filePath));
|
||||
}
|
||||
List<String> datas = new ArrayList<>();
|
||||
try (RandomAccessFile randomAccessFile = new RandomAccessFile(new File(csvFilePath), "r")) {
|
||||
try (RandomAccessFile randomAccessFile = new RandomAccessFile(new File(filePath), "r")) {
|
||||
if (readFileBean.getPointer() < 0) {
|
||||
readFileBean.setPointer(0);
|
||||
}
|
||||
@@ -365,29 +366,60 @@ public class FileUtil {
|
||||
//无数据
|
||||
return readFileBean.setDatas(new ArrayList<>());
|
||||
}
|
||||
|
||||
//csv文件处理
|
||||
com.yexuejc.base.pojo.CsvToBean csvToBean = getCsvToBean(readCls);
|
||||
readFileBean.setHeader(csvToBean.getHeader());
|
||||
if (csvToBean.hasHeader()) {
|
||||
//文件存在header,设置header优先,没设置使用文件的
|
||||
if (StrUtil.isNotEmpty(csvToBean.getHeader())) {
|
||||
//替换header
|
||||
datas.remove(0);
|
||||
datas.add(0, csvToBean.getHeader());
|
||||
} else {
|
||||
readFileBean.setHeader(datas.get(0));
|
||||
}
|
||||
} else {
|
||||
//文件不存在header,使用设置的
|
||||
datas.add(0, csvToBean.getHeader());
|
||||
}
|
||||
|
||||
List<T> dataList = readCsv(String.join("\n", datas), readCls, csvToBean.getDelimiter());
|
||||
List<T> dataList = readAfter.apply(datas);
|
||||
readFileBean.setDatas(dataList);
|
||||
return readFileBean;
|
||||
}
|
||||
|
||||
/**
|
||||
* 分段读取大文件(不解析)
|
||||
*
|
||||
* @param csvFilePath 文件路径
|
||||
* @param readFileBean 分段每次读取的bean 初始值需要设置每次读取的行数
|
||||
* @return 文件分页读取内容(每行为一个String对象)及读取信息
|
||||
*/
|
||||
public static ReadFileBean<String> readBigFile(String csvFilePath, ReadFileBean<String> readFileBean) throws IOException {
|
||||
return readBigFile(csvFilePath, readFileBean, (datas) -> datas);
|
||||
}
|
||||
|
||||
/**
|
||||
* 分段读取大文件(CSV格式)
|
||||
*
|
||||
* @param csvFilePath 文件路径
|
||||
* @param readFileBean 分段每次读取的bean 初始值需要设置每次读取的行数
|
||||
* @param <T> 读取结果类型bean
|
||||
* @return 文件分页读取内容(转bean后)及读取信息
|
||||
*/
|
||||
public static <T> ReadFileBean<T> readBigFile(String csvFilePath, ReadFileBean<T> readFileBean, Class<T> readCls) throws IOException {
|
||||
if (!csvFilePath.endsWith(TYPE_CSV)) {
|
||||
throw new IOException(String.format("[%s]文件不是CSV文件格式。", csvFilePath));
|
||||
}
|
||||
return readBigFile(csvFilePath, readFileBean, (datas) -> {
|
||||
//csv文件处理
|
||||
com.yexuejc.base.pojo.CsvToBean csvToBean = getCsvToBean(readCls);
|
||||
readFileBean.setHeader(csvToBean.getHeader());
|
||||
if (csvToBean.hasHeader()) {
|
||||
//文件存在header,设置header优先,没设置使用文件的
|
||||
if (StrUtil.isNotEmpty(csvToBean.getHeader())) {
|
||||
//替换header
|
||||
datas.remove(0);
|
||||
datas.add(0, csvToBean.getHeader());
|
||||
} else {
|
||||
readFileBean.setHeader(datas.get(0));
|
||||
}
|
||||
} else {
|
||||
//文件不存在header,使用设置的
|
||||
datas.add(0, csvToBean.getHeader());
|
||||
}
|
||||
try {
|
||||
return readCsv(String.join(NEW_LINE, datas), readCls, csvToBean.getDelimiter());
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取csv的header,使用注解{@link CsvToBean}
|
||||
*
|
||||
|
||||
@@ -249,6 +249,29 @@ public class JsonUtil {
|
||||
return pojo;
|
||||
}
|
||||
|
||||
/**
|
||||
* Json字符串转换为Java对象
|
||||
*
|
||||
* @param in 输入流
|
||||
* @param parametrized 容器类
|
||||
* @param parameterClasses 实际类
|
||||
* @return
|
||||
*/
|
||||
public static <T> T json2Obj(InputStream in, Class<T> parametrized, Class<?>... parameterClasses) {
|
||||
T pojo = null;
|
||||
JavaType javaType = jsonMapper.getTypeFactory().constructParametricType(parametrized, parameterClasses);
|
||||
try {
|
||||
pojo = jsonMapper.readValue(in, javaType);
|
||||
} catch (JsonParseException e) {
|
||||
log.warning("json to Object JsonParseException.\n" + StrUtil.printStackTrace(e));
|
||||
} catch (JsonMappingException e) {
|
||||
log.warning("json to Object JsonMappingException.\n" + StrUtil.printStackTrace(e));
|
||||
} catch (IOException e) {
|
||||
log.warning("json to Object IOException.\n" + StrUtil.printStackTrace(e));
|
||||
}
|
||||
return pojo;
|
||||
}
|
||||
|
||||
/**
|
||||
* Json字符串转换为Java-Map对象
|
||||
*
|
||||
@@ -291,29 +314,6 @@ public class JsonUtil {
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 = jsonMapper.getTypeFactory().constructParametrizedType(parametrized, parametrized,
|
||||
parameterClasses);
|
||||
try {
|
||||
pojo = jsonMapper.readValue(json, javaType);
|
||||
} catch (JsonParseException e) {
|
||||
log.warning("json to Object JsonParseException.\n" + StrUtil.printStackTrace(e));
|
||||
} catch (JsonMappingException e) {
|
||||
log.warning("json to Object JsonMappingException.\n" + StrUtil.printStackTrace(e));
|
||||
} catch (IOException e) {
|
||||
log.warning("json to Object IOException.\n" + StrUtil.printStackTrace(e));
|
||||
}
|
||||
return pojo;
|
||||
}
|
||||
|
||||
/**
|
||||
* 将任何对象转换为json
|
||||
@@ -322,15 +322,31 @@ public class JsonUtil {
|
||||
* @return 返回json
|
||||
*/
|
||||
public static String obj2Json(Object pojo) {
|
||||
String json = null;
|
||||
if (StrUtil.isEmpty(pojo)) {
|
||||
return "";
|
||||
}
|
||||
String json = "";
|
||||
try {
|
||||
json = jsonMapper.writeValueAsString(pojo);
|
||||
return jsonMapper.writeValueAsString(pojo);
|
||||
} catch (JsonProcessingException e) {
|
||||
log.warning("json to Object JsonProcessingException.\n" + StrUtil.printStackTrace(e));
|
||||
}
|
||||
return json;
|
||||
}
|
||||
|
||||
/**
|
||||
* 从bean到Map的转换
|
||||
* <p>
|
||||
* 例:Map map = JsonUtil.obj2Map(data);
|
||||
* </p>
|
||||
*
|
||||
* @param pojo bean
|
||||
* @return Map
|
||||
*/
|
||||
public static Map<?, ?> objToMap(Object pojo) {
|
||||
return json2Obj(obj2Json(pojo), Map.class);
|
||||
}
|
||||
|
||||
/**
|
||||
* 格式化输出
|
||||
*
|
||||
@@ -338,7 +354,7 @@ public class JsonUtil {
|
||||
* @return 格式化后的字符串
|
||||
*/
|
||||
public static String formatPrinter(Object obj) {
|
||||
String json = null;
|
||||
String json = "";
|
||||
try {
|
||||
json = jsonMapper.writerWithDefaultPrettyPrinter().writeValueAsString(obj);
|
||||
} catch (JsonProcessingException e) {
|
||||
|
||||
55
src/test/java/com/yexuejc/base/encrypt/AESTest.java
Normal file
55
src/test/java/com/yexuejc/base/encrypt/AESTest.java
Normal file
@@ -0,0 +1,55 @@
|
||||
package com.yexuejc.base.encrypt;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
|
||||
|
||||
public class AESTest {
|
||||
|
||||
@Test
|
||||
public void testEncrypt() throws Exception {
|
||||
String data = "Hello World!";
|
||||
AES aes = AES.builder()
|
||||
.setAlgorithm(AES.ALGORITHM.AES_CBC_PKCS5Padding)
|
||||
.setKey("hj7x89H$yuBI0456")
|
||||
.setIv("NIfb&95GUY86Gfgh")
|
||||
.setCharset(StandardCharsets.UTF_8);
|
||||
String encrypted = aes.encrypt(data);
|
||||
assertNotNull(encrypted);
|
||||
assertFalse(encrypted.isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDecrypt() throws Exception {
|
||||
String data = "SGVsbG8gV29ybGQh";
|
||||
AES aes = AES.builder()
|
||||
.setAlgorithm(AES.ALGORITHM.AES_CBC_PKCS5Padding)
|
||||
.setKey("hj7x89H$yuBI0456")
|
||||
.setIv("NIfb&95GUY86Gfgh")
|
||||
.setCharset(StandardCharsets.UTF_8);
|
||||
String decrypted = aes.decrypt(data);
|
||||
assertNotNull(decrypted);
|
||||
assertFalse(decrypted.isEmpty());
|
||||
assertEquals("Hello World!", decrypted);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEncryptAndDecrypt() throws Exception {
|
||||
String data = "张三";
|
||||
AES aes = AES.builder()
|
||||
.setAlgorithm(AES.ALGORITHM.AES_OFB_ISO10126Padding)
|
||||
.setKey("hj7x89H$yuBI0456")
|
||||
.setIv("NIfb&95GUY86Gfgh")
|
||||
.setCharset(StandardCharsets.UTF_8);
|
||||
String encrypt = aes.encrypt(data);
|
||||
System.out.println("加密:" + encrypt);
|
||||
String decrypt = aes.decrypt(encrypt);
|
||||
System.out.println("解密:" + decrypt);
|
||||
}
|
||||
|
||||
}
|
||||
70
src/test/java/com/yexuejc/base/util/DateUtilTest.java
Normal file
70
src/test/java/com/yexuejc/base/util/DateUtilTest.java
Normal file
@@ -0,0 +1,70 @@
|
||||
package com.yexuejc.base.util;
|
||||
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.time.LocalDate;
|
||||
import java.util.Date;
|
||||
import java.util.Locale;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
public class DateUtilTest {
|
||||
|
||||
@Test
|
||||
public void testParseDate() throws ParseException {
|
||||
String dateStr = "2022-03-20";
|
||||
String dateFormat = "yyyy-MM-dd";
|
||||
Date date = DateUtil.parseDate(dateStr, dateFormat);
|
||||
SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
|
||||
assertEquals(sdf.format(date), dateStr);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testParseLocalDate() throws ParseException {
|
||||
String dateStr = "2022-03-20";
|
||||
String dateFormat = "yyyy-MM-dd";
|
||||
LocalDate date = DateTimeUtil.parseLocalDate(DateUtil.parseDate(dateStr, dateFormat));
|
||||
assertEquals(date.toString(), dateStr);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFormatDateNow() {
|
||||
String dateFormat = "yyyy-MM-dd";
|
||||
String nowDateStr = DateUtil.formatDateNow(dateFormat);
|
||||
SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
|
||||
String nowDate = sdf.format(new Date());
|
||||
assertEquals(nowDate, nowDateStr);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFormatDate() {
|
||||
Date date = new Date();
|
||||
String dateFormat = "yyyy-MM-dd";
|
||||
String formattedDate = DateUtil.formatDate(date, dateFormat);
|
||||
SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
|
||||
assertEquals(sdf.format(date), formattedDate);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFormatDateWithLocale() {
|
||||
Date date = new Date();
|
||||
String dateFormat = "yyyy-MM-dd";
|
||||
Locale locale = Locale.JAPAN;
|
||||
String formattedDate = DateUtil.formatDate(date, dateFormat, locale);
|
||||
SimpleDateFormat sdf = new SimpleDateFormat(dateFormat, locale);
|
||||
assertEquals(sdf.format(date), formattedDate);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetDateByPlusDay() {
|
||||
int days = 2;
|
||||
String dateFormat = "yyyy-MM-dd";
|
||||
String plusDateStr = DateUtil.formatDate(DateUtil.datePlus(new Date(), days), dateFormat);
|
||||
LocalDate nowDate = LocalDate.now();
|
||||
LocalDate plusDate = nowDate.plusDays(days);
|
||||
assertEquals(plusDate.toString(), plusDateStr);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -4,6 +4,7 @@ import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import com.yexuejc.base.pojo.ReadFileBean;
|
||||
import com.yexuejc.base.util.bean.AppnodeCertCsvBean;
|
||||
@@ -54,19 +55,63 @@ public class FileUtilTest {
|
||||
}
|
||||
|
||||
private static void readCsvFile() throws IOException {
|
||||
String path = "F:\\coding\\yexuejc-base2\\src\\test\\java\\com\\yexuejc\\base\\util\\test.csv";
|
||||
String path = "F:\\coding\\yexuejc-base\\src\\test\\java\\com\\yexuejc\\base\\util\\test.csv";
|
||||
|
||||
List<AppnodeCertCsvBean> list = FileUtil.readCsv(path, AppnodeCertCsvBean.class, true, "enable,domain,protocol,deployHost,deployPath,uname,pwd,appnodeId", ',');
|
||||
System.out.println("***********************************************");
|
||||
System.out.println(JsonUtil.formatPrinter(list));
|
||||
System.out.println("条数:" + list.size());
|
||||
// List<AppnodeCertCsvBean> list = FileUtil.readCsv(path, AppnodeCertCsvBean.class, true, "enable,domain,protocol,deployHost,deployPath,uname,pwd,appnodeId", ',');
|
||||
// System.out.println("***********************************************");
|
||||
// System.out.println(JsonUtil.formatPrinter(list));
|
||||
// System.out.println("条数:" + list.size());
|
||||
|
||||
//直接把每行读取成字符串
|
||||
ReadFileBean<String> readFileBean2 = new ReadFileBean<>(2);
|
||||
ReadFileBean<String> bean2 = FileUtil.readBigFile(path, readFileBean2);
|
||||
System.out.println("直接把每行读取成字符串============================================");
|
||||
System.out.println(JsonUtil.formatPrinter(bean2));
|
||||
System.out.println("直接把每行读取成字符串============================================");
|
||||
|
||||
//自定义每行数据的处理
|
||||
ReadFileBean<AppnodeCertCsvBean> readFileBean1 = new ReadFileBean<>(2);
|
||||
ReadFileBean<AppnodeCertCsvBean> bean1 = FileUtil.readBigFile(path, readFileBean1, datas -> {
|
||||
if (readFileBean1.getStartRowNum() == 1) {
|
||||
datas.remove(0);//跳过第一行
|
||||
}
|
||||
return datas.stream().map(str -> {
|
||||
//自定义处理每一条数据
|
||||
String[] split = str.split(",");
|
||||
AppnodeCertCsvBean app = new AppnodeCertCsvBean();
|
||||
app.setEnable(getValue(split, 0));
|
||||
app.setDomain(getValue(split, 1));
|
||||
app.setProtocol(getValue(split, 2));
|
||||
app.setDeployHost(getValue(split, 3));
|
||||
app.setDeployPath(getValue(split, 4));
|
||||
app.setUname(getValue(split, 5));
|
||||
app.setPwd(getValue(split, 6));
|
||||
if (StrUtil.isNotEmpty(getValue(split, 7))) {
|
||||
app.setAppnodeId(Integer.valueOf(getValue(split, 7)));
|
||||
}
|
||||
return app;
|
||||
}).collect(Collectors.toList());
|
||||
});
|
||||
System.out.println("自定义每行数据的处理============================================");
|
||||
System.out.println(JsonUtil.formatPrinter(bean1));
|
||||
System.out.println("自定义每行数据的处理============================================");
|
||||
|
||||
//直接使用提供的csv文件读取
|
||||
ReadFileBean<AppnodeCertCsvBean> readFileBean = new ReadFileBean<>(2);
|
||||
do {
|
||||
ReadFileBean<AppnodeCertCsvBean> bean = FileUtil.readBigFile(path, readFileBean, AppnodeCertCsvBean.class);
|
||||
System.out.println("============================================");
|
||||
System.out.println("直接使用提供的csv文件读取============================================");
|
||||
System.out.println(JsonUtil.formatPrinter(bean));
|
||||
System.out.println("直接使用提供的csv文件读取============================================");
|
||||
} while (readFileBean.hasNext());
|
||||
|
||||
}
|
||||
|
||||
private static String getValue(String[] value, int index) {
|
||||
try {
|
||||
return value[index];
|
||||
} catch (Exception e) {
|
||||
return "";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,14 +2,16 @@ package com.yexuejc.base.util.bean;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
|
||||
import com.yexuejc.base.annotation.CsvToBean;
|
||||
import com.yexuejc.base.converter.IntegerNullValueDeserializer;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author: yexuejc
|
||||
* @date: 2024/2/27 10:40
|
||||
*/
|
||||
@CsvToBean(header = "enable,domain,protocol,deployHost,deployPath,uname,pwd,appnodeId", hasHeader = true)
|
||||
@CsvToBean(header = "enable,domain,protocol,deployHost,deployPath,uname,pwd,appnodeId")
|
||||
public class AppnodeCertCsvBean implements Serializable {
|
||||
/**是否生效:Y/N*/
|
||||
private String enable;
|
||||
@@ -37,6 +39,7 @@ public class AppnodeCertCsvBean implements Serializable {
|
||||
/**
|
||||
* appnode协议时:且远程部署时,对应的远程appnode的ApiNodeId
|
||||
*/
|
||||
@JsonDeserialize(using = IntegerNullValueDeserializer.class)
|
||||
private Integer appnodeId;
|
||||
|
||||
public String getEnable() {
|
||||
|
||||
10
src/test/java/com/yexuejc/base/util/test.csv
Normal file
10
src/test/java/com/yexuejc/base/util/test.csv
Normal file
@@ -0,0 +1,10 @@
|
||||
是否生效,域名,部署协议,部署服务器,部署证书位置,服务器账号,服务器密码,appnodeId
|
||||
N,kasm.mx.yexuejc.top,appnode,http://192.168.56.101:8888,,admin,admin,1
|
||||
N,kasm.mx.yexuejc.top,appnode,local,,,,
|
||||
N,shop.mx.yexuejc.top,appnode,local,,,,
|
||||
Y,cloud.yexuejc.top,ssh,118.126.109.109:10371,/home/frpuser/http/cert/,frpuser,yexuejc1,
|
||||
Y,blog.yexuejc.top,ssh,118.126.109.109:10371,/home/frpuser/http/cert/,frpuser,yexuejc1,
|
||||
Y,yexuejc.top,ssh,118.126.109.109:10371,/home/frpuser/http/cert/,frpuser,yexuejc1,
|
||||
Y,jenkins.yexuejc.top,ssh,118.126.109.109:10371,/home/frpuser/http/cert/,frpuser,yexuejc1,
|
||||
Y,git.yexuejc.top,ssh,118.126.109.109:10371,/home/frpuser/http/cert/,frpuser,yexuejc1,
|
||||
Y,nexus.yexuejc.top,ssh,118.126.109.109:10371,/home/frpuser/http/cert/,frpuser,yexuejc1,
|
||||
|
Reference in New Issue
Block a user