- 封装的工具类,主要用于LocalDateTime、Date、String 三者之间的相互转换。其中String与Date的转换还可以使用SimpleDateFormat,这里主要以DateTimeFormatter为主。
-
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.OffsetDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAccessor;
import java.util.Date;
public class DateUtil {
public static final String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";
/**
* String 转 LocalDateTime
* @param date
* @return
*/
public static LocalDateTime stringToLocalDateTime(String date) {
//也可以自己定义格式
//DateTimeFormatter formatter = DateTimeFormatter.ISO_OFFSET_DATE_TIME;
DateTimeFormatter formatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME;
LocalDateTime dateTime = LocalDateTime.parse(date, formatter);
return dateTime;
}
/**
* localDateTime 转 时间戳
* @param localDateTime
* @return
*/
public static Long localDateTimeToTimeStamp(LocalDateTime localDateTime) {
return localDateTime.toInstant(ZoneOffset.of("+8")).toEpochMilli();
}
/**
* String 转 Date
* @param happenTime
* @param dateFormat
* @return
*/
public static Date stringToDate(String happenTime, String dateFormat) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(dateFormat);
OffsetDateTime offsetDateTime = OffsetDateTime.parse(happenTime, formatter);
// 转换为java.util.Date对象
Date date = Date.from(offsetDateTime.toInstant());
return date;
}
/**
* Date 转字符串 用SimpleDateFormat
*
* @param time
* @return
*/
public static String format(Date time) {
return format(time, DATE_FORMAT);
}
public static String format(Date time, String pattern) {
SimpleDateFormat formatter = new SimpleDateFormat(pattern);
return formatter.format(time);
}
/**
* Date 转字符串 用DateTimeFormatter
*/
public static String format2(Date time, String pattern) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
return formatter.format(date2LocalDateTime(time));
}
/**
* LocalDateTime 转字符串
*/
public static String format3(LocalDateTime time, String pattern) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
return formatter.format(time);
}
/**
* Date 转 LocalDateTime
*/
public static LocalDateTime date2LocalDateTime(Date date) {
return date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
}
/**
* LocalDateTime 转Date
*/
public static Date localDateTime2Date(LocalDateTime time) {
return Date.from(time.atZone(ZoneId.systemDefault()).toInstant());
}
}
- 两个实体类,主要涉及对象与json串之间的转换(涉及日期类型),当对象含有LocalDateTime、LocalDate、LocalTime类型的属性时,在利用ObjectMapper转换成Json字符串时,需要给objectMapper添加一些格式类型。
package com.example.restservice.pojo;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.format.annotation.DateTimeFormat;
import java.util.Date;
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class Animal {
private String name;
private Integer age;
@JsonFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss", timezone = "GMT+8")
private Date birthday;
}
package com.example.restservice.pojo;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.Date;
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class AnimalCC {
private String name;
private Integer age;
@JsonFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss", timezone = "GMT+8")
private LocalDateTime birthday;
private LocalDate localDate;
@JsonFormat(pattern = "HH:mm:ss:SSS", timezone = "GMT+8")
private LocalTime localTime;
}
- 一些测试例子
-
package com.example.restservice.util;
import com.example.restservice.pojo.Animal;
import com.example.restservice.pojo.AnimalCC;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.*;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalTimeSerializer;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
public class Test {
public static void main(String[] args) throws JsonProcessingException, ParseException {
ObjectMapper objectMapper = new ObjectMapper();
JavaTimeModule javaTimeModule = new JavaTimeModule();
javaTimeModule.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
javaTimeModule.addSerializer(LocalDate.class, new LocalDateSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
javaTimeModule.addSerializer(LocalTime.class, new LocalTimeSerializer(DateTimeFormatter.ofPattern("HH:mm:ss")));
objectMapper.registerModule(javaTimeModule);
Animal animal = new Animal();
animal.setAge(3);
animal.setName("小狗");
animal.setBirthday(new Date());
System.out.println(animal);
String s = objectMapper.writeValueAsString(animal);
System.out.println(s);
System.out.println("=====================================");
System.out.println(animal.getBirthday());
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy/MM/dd-HH:mm:ss.SSS");
System.out.println(dateTimeFormatter.format(LocalDateTime.now()));
String dateTime1 = "2021-01-01T12:12:12.123+08:00";
SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS+08:00");
Animal animal1 = new Animal();
animal1.setAge(3);
animal1.setName("小狗");
animal1.setBirthday(simpleDateFormat2.parse(dateTime1));
System.out.println(simpleDateFormat2.format(new Date()));
System.out.println(animal);
String s8 = objectMapper.writeValueAsString(animal);
System.out.println(s8);
System.out.println("=====================================");
AnimalCC animalCC = new AnimalCC();
animalCC.setAge(3);
animalCC.setName("小狗");
animalCC.setBirthday(DateUtil.date2LocalDateTime(new Date()));
animalCC.setLocalDate(LocalDateTime.now().toLocalDate());
animalCC.setLocalTime(LocalDateTime.now().toLocalTime());
System.out.println(animalCC);
String s2 = objectMapper.writeValueAsString(animalCC);
System.out.println(s2);
System.out.println("=====================================");
String dateTime = "2021-01-01T12:12:12";
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
Date date = simpleDateFormat.parse(dateTime);
String s3 = simpleDateFormat.format(date);
System.out.println(date);
System.out.println(s3);
String s1 = objectMapper.writeValueAsString(date);
System.out.println(s1);
Date from = Date.from(DateUtil.stringToLocalDateTime(dateTime).atZone(ZoneId.systemDefault()).toInstant());
System.out.println(from);
LocalDateTime localDateTime = DateUtil.stringToLocalDateTime(dateTime);
System.out.println(localDateTime);
System.out.println("**********************************************");
System.out.println(DateUtil.format3(LocalDateTime.now(), "yyyy-MM-dd HH:mm:ss"));
System.out.println(DateUtil.format2(new Date(), "yyyy-MM-dd HH:mm:ss.SSS+08:00"));
System.out.println("**********************************************");
String animalStr = "{\"name\":\"小狗\",\"age\":3,\"birthday\":\"2021-01-01T12:12:12\"}";
String animalStr2 = "{\"name\":\"小狗\",\"age\":3,\"birthday\":\"2021-01-01T12:12:12\",\"localDate\":\"2021-01-01\",\"localTime\":\"12:12:12.333\"}";
Animal animal2 = objectMapper.readValue(animalStr, Animal.class);
AnimalCC animalCC2 = objectMapper.readValue(animalStr2, AnimalCC.class);
System.out.println(animal2);
System.out.println(animalCC2);
}
}