一、类型转换
1.Map 与 Bean 之间的转换
<dependency>
<groupId>com.alibaba.fastjson2</groupId>
<artifactId>fastjson2</artifactId>
<version>2.0.51</version>
</dependency>
@Data
public class TestClass {
private String name;
private int age;
private String address;
}
@Test
public void test2(){
Map<String, Object> map = new HashMap<>();
map.put("name","张三");
map.put("age",18);
map.put("address","北京");
TestClass testClass = JSONObject.parseObject(JSONUtil.toJsonStr(map), TestClass.class);
System.out.println(testClass);
Map<String, Object> jsonObject = JSONObject.parseObject(JSONUtil.toJsonStr(testClass));
System.out.println(jsonObject);
System.out.println(jsonObject.get("name"));
}
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.8.25</version>
</dependency>
@Test
public void test1(){
Map<String, Object> map = new HashMap<>();
map.put("name","张三");
map.put("age",18);
map.put("address","北京");
TestClass testClass = JSONUtil.toBean(JSONUtil.toJsonStr(map), TestClass.class);
System.out.println(testClass);
Map<String, Object> jsonObject = JSONUtil.parseObj(JSONUtil.toJsonStr(testClass));
System.out.println(jsonObject);
System.out.println(jsonObject.get("name"));
Map<String, Object> params = BeanUtil.beanToMap(testClass);
System.out.println(params);
}
二、日期工具
1.获取两个时间节点之间的月份列表
public static List<String> getMonthListByDate(String startTime, String endTime) {
ArrayList<String> result = new ArrayList<>();
try {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
Calendar startDate = Calendar.getInstance();
Calendar endDate = Calendar.getInstance();
startDate.setTime(sdf.parse(startTime));
startDate.set(startDate.get(Calendar.YEAR), startDate.get(Calendar.MONTH), 1);
endDate.setTime(sdf.parse(endTime));
endDate.set(endDate.get(Calendar.YEAR), endDate.get(Calendar.MONTH), 2);
Calendar curr = startDate;
while (curr.before(endDate)) {
result.add(sdf.format(curr.getTime()));
curr.add(Calendar.MONTH, 1);
}
} catch (ParseException e) {
e.printStackTrace();
}
return result;
}
public static List<String> getMonthListByDate(String startTime, String endTime) {
if(StrUtil.isBlank(startTime) || StrUtil.isBlank(endTime)) {
return Collections.emptyList();
}
ArrayList<String> result = new ArrayList<>();
Date startDate = DateUtil.parse(startTime);
Date endDate = DateUtil.parse(endTime);
while (startDate.compareTo(endDate)<=0) {
result.add(DateUtil.format(startDate,DatePattern.NORM_MONTH_PATTERN));
startDate = DateUtil.offsetMonth(startDate,1);
}
return result;
}
2.判断当前时间是否在某个时间范围内
public static boolean isEffectiveDate(Date nowTime, Date startTime, Date endTime) {
if(nowTime == null || startTime == null || endTime == null){
return false;
}
if (nowTime.getTime() == startTime.getTime()
|| nowTime.getTime() == endTime.getTime()) {
return true;
}
Calendar date = Calendar.getInstance();
date.setTime(nowTime);
Calendar begin = Calendar.getInstance();
begin.setTime(startTime);
Calendar end = Calendar.getInstance();
end.setTime(endTime);
if (date.after(begin) && date.before(end)) {
return true;
} else {
return false;
}
}
public static boolean isEffectiveDate(Date nowTime, Date startTime, Date endTime) {
if(nowTime == null || startTime == null || endTime == null){
return false;
}
if (nowTime.getTime() == startTime.getTime()
|| nowTime.getTime() == endTime.getTime()) {
return true;