//获得当前时间戳,long类型
long timeStamp = System.currentTimeMillis();
//要转换成的时间格式
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String sd = sdf.format(new Date(timeStamp)); //时间戳转换成时间
@GetMapping(value = "/chats/{chatId}/messages")
public Result getBeforeMessages(@PathVariable("chatId") Long chatId,@RequestParam("maxCreatedAt") Long maxCreatedAt, HttpServletRequest request){...}
得这样访问 127.0.0.1:8080/chats/3/messages?maxCreatedAt=1537703899000
如果想这样访问 127.0.0.1:8080/chats/3/messages?maxCreatedAt=2018-09-23 19:58:21
就得在springmvc中配置字符串转时间转换器,请求的字符串必须为“2018-09-23 19:58:21”这样
在controller中添加注解@InitBinder
/**
* @Description: 将前端传递的字符串2018-09-23 19:58:21转换为Date类型
* @Params [binder]
* @return: void
* @Date: 2018/9/25
*/
@InitBinder
public void initBinder(WebDataBinder binder){
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//严格限制日期转换
sdf.setLenient(false);
//true:允许输入空值,false:不能为空值
binder.registerCustomEditor(.class,new CustomDateEditor(sdf,true));
}
public Result getBeforeMessages(@PathVariable("chatId") Long chatId,@RequestParam("maxCreatedAt") Date maxCreatedAt, HttpServletRequest request){。。。}
如果此时访问127.0.0.1:8080/chats/3/messages?maxCreatedAt=1537703899000会报400错误