项目上总会遇到一些通用的方法,每次使用去找不免会花费不必要的时间,这篇博客旨在不断积累有用的工具方法
一、工具类参考文档
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 now = LocalDateTime.now(); System.out.println("当前时刻 = " + now); System.out.println("当前年份 = " + now.getYear()); System.out.println("当前月份值 = " + now.getMonthValue()); System.out.println("now.getMonth() = " + now.getMonth()); System.out.println("now.getDayOfMonth() = " + now.getDayOfMonth()); System.out.println("now.getDayOfWeek() = " + now.getDayOfWeek()); System.out.println("now.getChronology() = " + now.getChronology()); System.out.println("now.getHour() = " + now.getHour()); System.out.println("now.getSecond() = " + now.getSecond()); System.out.println("now.getNano() = " + now.getNano()); System.out.println("now.getMinute() = " + now.getMinute());
System.out.println(now.format(DateTimeFormatter.ISO_DATE)); System.out.println(now.format(DateTimeFormatter.BASIC_ISO_DATE)); System.out.println(now.format(DateTimeFormatter.ofPattern("yyyy/MM/dd"))); System.out.println(now.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))); 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)); }
|
二、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); }
}
|