Spring MVC日期自动转时间戳问题
日期自动转换成时间戳格式解决办法(亲测有效)
将日期转成字符串,然后返回给前端
mybatis查询的时候返回字符串类型日期

解决方法如下:
1.示例代码:
Date date = new Date();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String time = simpleDateFormat.format(date);
//Date类型转String类型
Date d = simpleDateFormat.parse(time);
套用实例代码,把时间放进去
2.业务代码片段:
for (MyOrder myOrder : myOrders) {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Map map = new HashMap();
if (myOrder.getIstype() == 1) {
Collection collection = collectionDao.selectByPrimaryKey(myOrder.getCollid());
if (collection != null) {
map.put("collid", myOrder.getCollid());
map.put("id", myOrder.getId());
map.put("name", collection.getName());
map.put("img", imgConfig.getUrl() + collection.getImg());
map.put("no", myOrder.getOrderno());
map.put("createtime", simpleDateFormat.format(myOrder.getCreatetime()));
map.put("endtime", simpleDateFormat.format(myOrder.getEndtime()));

3.返回结果:


本文介绍如何在SpringMVC中将日期格式自动转换为时间戳格式,并通过MyBatis查询返回字符串类型的日期。提供了具体实现代码示例,包括使用SimpleDateFormat进行格式化。
1270

被折叠的 条评论
为什么被折叠?



