mapstruct的用法-dateFormat
Posted 二十六画生的博客
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mapstruct的用法-dateFormat相关的知识,希望对你有一定的参考价值。
可先阅读:mapstruct的用法-qualifiedByName使用的类都是这里的。
1 源类
public class AreaPO
private String cityName;
private Integer haveAir;
private Double pm25;
private String pm10Str;
private Date updatedTime;
2 目标类
public class AreaVO
private String cityName;
private Integer haveAir;
private Double pm25;
private String pm25Str;
private Double pm10;
private String updatedTime;
3 Mapper
@Mapping(source = "updatedTime", target = "updatedTime", dateFormat = "yyyy-MM-dd HH:mm:ss")
AreaVO areaPO2areaVO(AreaPO areaPO);
4 Impl
package com.weather.weatherexpert.common.model.mapper;
import com.weather.weatherexpert.common.model.po.AreaPO;
import com.weather.weatherexpert.common.model.vo.AreaVO;
import java.text.SimpleDateFormat;
public class ConvertMapperImpl implements ConvertMapper
public ConvertMapperImpl()
public AreaVO areaPO2areaVO(AreaPO areaPO)
if (areaPO == null)
return null;
else
AreaVO areaVO = new AreaVO();
if (areaPO.getUpdatedTime() != null)
areaVO.setUpdatedTime((new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).format(areaPO.getUpdatedTime()));
areaVO.setCityName(areaPO.getCityName());
areaVO.setHaveAir(areaPO.getHaveAir());
areaVO.setPm25(areaPO.getPm25());
return areaVO;
5 测试
AreaPO areaPO3 = new AreaPO("忻州", new Date());
AreaVO areaVO3 =
ConvertMapper.INSTANCE.areaPO2areaVO(areaPO3);
logger.info("JSON.toJSONString(areaVO3):" + JSON.toJSONString(areaVO3));
输出:
JSON.toJSONString(areaVO3):"cityName":"忻州","updatedTime":"2018-12-25 21:30:50"
注意类型,是Date转换为String
以上是关于mapstruct的用法-dateFormat的主要内容,如果未能解决你的问题,请参考以下文章
完成字符串和时间对象的转化(DateFormat)(以及日历Calendar用法)
详解Java中格式化日期的DateFormat与SimpleDateFormat类