程序小牛

来了就随便看看,虽然没有什么好看的~~

工具方法集合

项目上总会遇到一些通用的方法,每次使用去找不免会花费不必要的时间,这篇博客旨在不断积累有用的工具方法

一、工具类参考文档

1 、hutool

hutool官网

hutool参考文档

API 接口文档

二、时间处理

1、常规使用

LocalDate实用方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
public void testDateUtilMethod(){
//localDateTime 方法
LocalDateTime now = LocalDateTime.now();
System.out.println("当前时刻 = " + now);//当前时刻 = 2021-12-16T10:09:30.898
System.out.println("当前年份 = " + now.getYear());//当前年份 = 2021
System.out.println("当前月份值 = " + now.getMonthValue());//当前月份值 = 12
System.out.println("now.getMonth() = " + now.getMonth());//now.getMonth() = DECEMBER
System.out.println("now.getDayOfMonth() = " + now.getDayOfMonth());//now.getDayOfMonth() = 16
System.out.println("now.getDayOfWeek() = " + now.getDayOfWeek());//now.getDayOfWeek() = THURSDAY
System.out.println("now.getChronology() = " + now.getChronology());//now.getChronology() = ISO
System.out.println("now.getHour() = " + now.getHour());//now.getHour() = 10
System.out.println("now.getSecond() = " + now.getSecond());//now.getSecond() = 30
System.out.println("now.getNano() = " + now.getNano());//now.getNano() = 898000000
System.out.println("now.getMinute() = " + now.getMinute());//now.getMinute() = 9

//format DateTimeFormatter 时间格式化
System.out.println(now.format(DateTimeFormatter.ISO_DATE));//2021-12-16
System.out.println(now.format(DateTimeFormatter.BASIC_ISO_DATE));//20211216
System.out.println(now.format(DateTimeFormatter.ofPattern("yyyy/MM/dd")));//2021/12/16
System.out.println(now.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));//2022-03-28 13:54:22

//3天前日期
LocalDateTime.now().minusDays(3).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));

//解析时间字符串
LocalDateTime parseDate = LocalDateTime.parse("2002--01--02 11:21", DateTimeFormatter.ofPattern("yyyy--MM--dd HH:mm"));
System.out.println(parseDate.format(DateTimeFormatter.BASIC_ISO_DATE));//20020102
}

二、JSON转换

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package com.xunzhi.scrm.utils.helper;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.datatype.jdk8.Jdk8Module;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.fasterxml.jackson.module.paramnames.ParameterNamesModule;

public class JacksonUtils {
private JacksonUtils() {
}

public static String toJsonString(Object object) throws JsonProcessingException {
ObjectMapper mapper = new ObjectMapper().registerModule(new JavaTimeModule())
.registerModule(new ParameterNamesModule()).registerModule(new Jdk8Module())
.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
return mapper.writeValueAsString(object);
}

public static <T> T toJavaObject(String jsonString, Class<T> valueType) throws JsonProcessingException {
ObjectMapper mapper = new ObjectMapper().registerModule(new JavaTimeModule())
.registerModule(new ParameterNamesModule()).registerModule(new Jdk8Module())
.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
return mapper.readValue(jsonString, valueType);
}

public static <T> T toJavaObject(String jsonString, TypeReference<T> typeReference) throws JsonProcessingException {
ObjectMapper mapper = new ObjectMapper().registerModule(new JavaTimeModule())
.registerModule(new ParameterNamesModule()).registerModule(new Jdk8Module())
.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
return mapper.readValue(jsonString, typeReference);
}

}