关于接口调用的日期格式化问题

这两天改bug时候遇到一个很奇怪的问题,暂时没找到具体原因,先记录下来。

public class AirTicketResourceBO implements Serializable{
    private static final long serialVersionUID = 5469201963274216101L;

    private AirTicketResource po; 
    private List<AirTicketLeg> legList;
    private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    private SimpleDateFormat sdfTime = new SimpleDateFormat("yyyy-MM-dd HH:mm");
    private SimpleDateFormat legFormat = new SimpleDateFormat("HH:mm");


    public String getStartDate(){
        System.out.println("----------------------------------------------------"+DateUtils.format(this.po.getStartDate()));
        System.out.println("----------------------------------------------------"+DateUtils.format(po.getStartDate(), "yyyy-MM-dd"));
        return DateUtils.format(po.getStartDate(), "yyyy-MM-dd");
//      return DateUtils.format(this.po.getStartDate());
    }
    public String getEndDate(){
        if (this.po.getEndDate()==null){return "";}
//      return DateUtils.format(this.po.getEndDate());
        return DateUtils.format(po.getEndDate(), "yyyy-MM-dd");
    }

    ...
    public String getLegHtml() throws ParseException{
        if (this.legList == null){return "";}
        StringBuffer sb = new StringBuffer("<table class='air_ticket_leg'>");
        for(int i=0; i<this.legList.size(); i++){
            AirTicketLeg leg = this.legList.get(i);
            sb.append("<tr><td width='75'>").append(DateUtils.format(leg.getDepDate(),"yyyy-MM-dd")).append("</td><td>");
            sb.append(leg.getAirCode()).append("</td><td>");
            sb.append(leg.getDepCity()).append("-").append(leg.getArrCity()).append("</td>");
            String depTime=(leg.getDepTime()==null)?"":DateUtils.format(leg.getDepTime(),"HH:mm");
            String arrTime=(leg.getArrTime()==null)?"":DateUtils.format(leg.getArrTime(),"HH:mm");
            sb.append("<td>").append(depTime).append("-").append(arrTime).append("</td></tr>");
        }
        sb.append("</table>");
        //格式化方式对比
//      for(int i=0; i<this.legList.size(); i++){
//          AirTicketLeg leg = this.legList.get(i);
//          sb.append("<tr><td width='75'>").append(sdf.format(leg.getDepDate())).append("</td><td>");
//          sb.append(leg.getAirCode()).append("</td><td>");
//          sb.append(leg.getDepCity()).append("-").append(leg.getArrCity()).append("</td>");
//          String depTime=(leg.getDepTime()==null)?"":legFormat.format(leg.getDepTime());
//          String arrTime=(leg.getArrTime()==null)?"":legFormat.format(leg.getArrTime());
//          sb.append("<td>").append(depTime).append("-").append(arrTime).append("</td></tr>");
//      }

        return sb.toString();

}
//dubbo接口
@Override
    public TicketListResult ticketList(Integer bizId, Integer id) {

        AirTicketResource resourcePo = airTicketResourceBiz.findResource(id, bizId);
        AirTicketResourceBO resourceBo = new AirTicketResourceBO(resourcePo);
        resourceBo.setLegList(airTicketResourceBiz.findLegsByResourceId(resourceBo.getPo().getId()));
        List<AirTicketRequest> requestList = airTicketRequestBiz.findRequestsByResource(id, bizId);
        List<AirTicketRequestBO> boList = new ArrayList<AirTicketRequestBO>();
        for (int i=0; i<requestList.size(); i++){
            AirTicketRequest po = requestList.get(i);
            po.setResource(resourcePo);
            AirTicketRequestBO bo = new AirTicketRequestBO(po);
            bo.setGroupOrder(groupOrderBiz.findById(po.getGroupOrderId()));
            bo.go.setGroupOrderGuestList(groupOrderGuestBiz.selectByOrderId(po.getGroupOrderId())); 
            boList.add(bo);
        }

        TicketListResult result = new TicketListResult();
        result.setResourceBo(resourceBo);
        result.setBoList(boList);
        return result;
    }
//Controller方法
@RequestMapping("ticketList.htm")
    /* @RequiresPermissions(PermissionConstants.AIR_TICKET_RESOURCE) */
    public String ticketList(HttpServletRequest request, HttpServletResponse reponse, ModelMap model, Integer id){
        Integer bizId = WebUtils.getCurBizId(request);
        TicketListResult result = requestFacade.ticketList(bizId, id);
        model.addAttribute("resource", result.getResourceBo());
        model.addAttribute("boList", result.getBoList());
        return "airticket/ticket_list";
    }

修改前的输出结果:
dubbo接口:

<table class='air_ticket_leg'><tr><td width='75'>

2017-02-02
----------

</td><td>MU5734</td><td>昆明-丽江</td><td>

19:40-20:35
-----------

</td></tr></table>

controller方法:

<table class='air_ticket_leg'><tr><td width='75'>

17-2-2 上午12:00
--------------

</td><td>MU5734</td><td>昆明-丽江</td><td>

17-2-2 下午7:40-17-2-2 下午8:35
---------------------------

</td></tr></table>

修改前是使用PO对象内部定义的SimpleDateFormat 类,修改后使用的自定义工具类DateUtils,但是结果完全不一样。
而另一个系统中的AirTicketResourceBO 类放在web工程中,没有实现序列化接口,格式化结果没有问题。

已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 创作都市 设计师:CSDN官方博客 返回首页