今天做Cookie练习时碰到了java.lang.IllegalArgumentException异常,记录下来,防止以后再次出错。
代码如下:
Date date = new Date();
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
String currentTime = format.format(date);
Cookie cookie = new Cookie("islasttime",currentTime);
cookie.setMaxAge(10000);// 设置生存时间为10000秒
response.addCookie(cookie);
这里出现了异常,原因是:
SimpleDateFormat format = new SimpleDateFormat(“yyyy-MM-dd hh:mm:ss”);
dd和hh之间有空格,需要进行转化
解决方案如下:将空格换为-即可
SimpleDateFormat format = new SimpleDateFormat(“yyyy-MM-dd-hh:mm:ss”);