Json格式化
导包
implementation 'com.alibaba:fastjson:1.2.78'
使用注解
@JSONField(format = "yyyy/MM/dd HH:mm:ss")
private Date createDate;
测试
@Test
void dateTest(){
UserEntity user = new UserEntity("1", new Date(System.currentTimeMillis()));
System.err.println(JSON.toJSONString(user));
}
结果
{"createDate":"2022/07/19 13:21:10","username":"1"}
SimpleDateFormat 日期化格式化
@Test
void contextLoads() throws FileNotFoundException, ParseException {
Date date = new Date(System.currentTimeMillis());
DateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
//日期转字符串
String DateString = simpleDateFormat.format(date);
System.err.println(DateString);
//字符串转日期
Date dateParseDate = simpleDateFormat.parse(DateString);
System.err.println(dateParseDate);
}