java获取30天后时间-时间格式注解-时间格式转换

注解

@JsonFormat(timezone = "GMT+8",pattern = "yyyy-MM-dd")
@DateTimeFormat(pattern="yyyy-MM-dd")
private Date date;

@JsonFormat:获取数据库时间格式化一下,因为数据库时区与标准时区相差八小时 所以GMT+8 后面即是时间格式如:yyyy-MM-dd
@DateTimeFormat:Date类型想要限定请求传入时间格式时,可以通过@DateTimeFormat来指定,但请求传入参数与指定格式不符时,会返回400错误。
如果在Bean属性中有Date类型字段,想再序列化转为指定格式时,也可用@DateTimeFormat来指定想要的格式

时间格式转换

//将当前时间转换为指定格式的String字符串
new SimpleDateFormat("yyyy-MM-dd").format(new Date());
//将String字符串转换为指定格式的Date,需要捕获异常
new SimpleDateFormat("yyyy-MM-dd").parse("2020-07-21");

获取三十天后时间

方法一

//获取当前系统时间
Calendar cal = Calendar.getInstance();
//将时间增加三十天
        cal.add(Calendar.DATE, 30);
 //获取改变后的时间
       	cal.getTime();

方法二

//java8新特性
LocalDateTime.now().minus(-30, ChronoUnit.DAYS);

方法三

//工具类
DateUtils.addDays(new Date(), 30);

可以利用时间格式转换成想要的格式

猜你喜欢

转载自blog.csdn.net/ycg_x_y/article/details/107896738