1.采取获取当前时间出现的问题
假如两个用户处于不同的时区,那么获取当前时间可能出现时间标准不一致的问题。
因此不能直接进行new Date获取当前时间。
考虑时区进行format
2.标准时间,+08:00 指标准时间加8小时,也就是北京时间
ZonedDateTime now = ZonedDateTime.ofInstant(Instant.ofEpochMilli(System.currentTimeMillis()), DateTimeUtils.cnZoneOffset());
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
sdf.setTimeZone(CN_TIME_ZONE);
sdf.format(new Date(now.toInstant().toEpochMilli()));
String createdAt = sdf.toString();
ViewedGoods viewedGoods = mapper.selectByKeyandCreateAt(userId,goods.getChannelCode(),goods.getSkuId(),createdAt);
if(viewedGoods != null) {
return;
}
SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");
sdf2.setTimeZone(CN_TIME_ZONE);
sdf2.format(new Date(now.toInstant().toEpochMilli()));
Date date = sdf2.parse(sdf2.toString());
3.Java之SimpleDateFormat日期格式转换(Date 和 String 类型之间的转换)
public class SimpleDateFormatDemo {
public static void main(String[] args) throws ParseException {
// TODO Auto-generated method stub
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");
//Date指定格式:yyyy-MM-dd HH:mm:ss:SSS
Date date = new Date();//创建一个date对象保存当前时间
String dateStr = simpleDateFormat.format(date);//format()方法将Date转换成指定格式的String
System.out.println(dateStr);//2018-08-24 15:37:47:033
String string = "2018-8-24 12:50:20:545";
Date date2 = simpleDateFormat.parse(string);//调用parse()方法时
//注意 传入的格式必须符合simpleDateFormat对象的格式,即"yyyy-MM-dd HH:mm:ss:SSS" 否则会报错!!
System.out.println(date2);//Fri Aug 24 12:50:20 CST 2018
}
}
4.在springmvc中 @RequestMapping(value={"", “/”})表示的意思
如果@RequestMapping() 表示:请求地址默认为绝对地址
@RequestMapping(value={"", “/”})
表示:请求的地址是 绝对地址 localhost:8080/bussys
或者 绝对地址+“/” localhost:8080/bussys/