mirror of
https://gitee.com/jzsw-it/yexuejc-base.git
synced 2025-06-03 04:24:05 +08:00
[update] JsonUtil 增加objToMap;优化obj2Json
DateUtil 标准化日期时间的转换函数
This commit is contained in:
parent
0954eb64b5
commit
1415200b1a
@ -6,7 +6,8 @@ yexuejc-base 更新记录
|
||||
**branch:** jre11 <br/>
|
||||
**update:** <br/>
|
||||
1. [FileUtil.java](src/main/java/com/yexuejc/base/util/FileUtil.java)增加读取大文件自定义方法和单纯读取方法
|
||||
2.
|
||||
2. JsonUtil 增加objToMap;优化obj2Json
|
||||
3. DateUtil 标准化日期时间的转换函数
|
||||
---
|
||||
|
||||
#### version :1.5.2-jre11
|
||||
|
@ -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);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取本周的日期
|
||||
|
@ -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) {
|
||||
|
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);
|
||||
}
|
||||
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user