简单日期格式解析不起作用

时间:2013-07-24 20:14:49

标签: java groovy

我在下面的方法中发布了2个日期try / catch块。 当我点击这个方法时,我得到一个空指针异常:

private List<HashMap<String,Object>> getOrderHistoryData(final HttpServletRequest request)
{
    List<HashMap<String,Object>> productInfo = new ArrayList<HashMap<String,Object>>();
    JsCustomerSession sess = (JsCustomerSession) getRequestHelper().getCustomerSession(request);
    JsCustomer customer = (JsCustomer) sess.getShopper().getCustomer();
    ExtStore store = (ExtStore) sess.getShopper().getCurrentShoppingCart().getStore();
    RemoteOrderHistoryService orderHistoryService = remoteServiceLocator.getOrderHistoryService(store);

    DateFormat formatter = new SimpleDateFormat("MM-dd-yyyy", Locale.ENGLISH);
    Calendar cal = Calendar.getInstance();
    String defaultEnd = formatter.format(cal.getTime());
    log.debug("Your default end string is: " + defaultEnd);
    cal.add(Calendar.DATE, -30);

    String defaultStart = formatter.format(cal.getTime());
    log.debug("Your defaultStart string is: " + defaultStart);
    String startStr = ServletRequestUtils.getStringParameter(request, "start", defaultStart);
    String endStr = ServletRequestUtils.getStringParameter(request, "end", defaultEnd);
    log.debug("Your start string is: " + startStr);

    Date end;
    try {
        end = formatter.parse(endStr);
    } catch (ParseException e) {
        e.printStackTrace();
        end = new Date();
    }

    String searchString = ServletRequestUtils.getStringParameter(request, "searchString", "").trim();
    String show = ServletRequestUtils.getStringParameter(request, "show", "");
    String searchType = ServletRequestUtils.getStringParameter(request, "search-filter", "");

    if(startStr.equals(defaultStart)) 
    {
        if(searchString != "") 
        {
            Calendar newStart = Calendar.getInstance();
            newStart.add(Calendar.YEAR, -25);
            startStr = formatter.format(newStart.getTime());
        } 
        else 
        {
            startStr = defaultStart;
        }   
    }

    if (show == "")
    {
        show = "all";
    }

    if(store.getPosType().equals(ExtStoreImpl.POS_TYPE_INFOR) || store.getPosType().equals(ExtStoreImpl.POS_TYPE_DATABASE))
    {
        searchString  += "show: " + show + "type: " + searchType;
    }


    Date start;
    try {
        start = formatter.parse( startStr );
    } catch (ParseException e) {
        e.printStackTrace();
        start = new Date();
    }

    log.debug("SEARCHING FROM " + start + " TO " + end);
    log.debug("SEARCHING FOR " + searchString);
    log.debug("SEARCHING TYPE " + searchType);

    List<OrderHistoryBean> ohs = orderHistoryService.getOrderHistory(customer, start, end, searchString);
    HashMap<String,Object> excelRowData = new HashMap<String,Object>();

    if (store.getPosType().equals(ExtStoreImpl.POS_TYPE_ECLIPSE))
    {
        for(OrderHistoryBean orderHistory : ohs)
        {
            String shipToFirstName = orderHistory.getShipTo().getFirstName().toString();
            String shipToLastName = orderHistory.getShipTo().getLastName().toString();
            String shipToFirstAndLastName = shipToFirstName + "" + shipToLastName;

            excelRowData.put("Order#", orderHistory.getReferenceNumber());
            excelRowData.put("PO#", orderHistory.getPoNumber());
            excelRowData.put("Ordered", orderHistory.getPostedDate());
            excelRowData.put("Shipped", orderHistory.getShippedDate());
            excelRowData.put("Status", orderHistory.getStatus());
            excelRowData.put("Ship To", shipToFirstAndLastName);
            excelRowData.put("Amount", orderHistory.getAmount());
            productInfo.add(excelRowData);
        }
    } 
    else if (store.getPosType().equals(ExtStoreImpl.POS_TYPE_DATABASE))
    {
        for(OrderHistoryBean orderHistory : ohs)
        {
            excelRowData.put("Reference#", orderHistory.getOrderNumber());
            excelRowData.put("Posted Date", orderHistory.getPostedDate());
            excelRowData.put("PO/Job#", orderHistory.getPoNumber());
            excelRowData.put("Amount", orderHistory.getAmount());
            productInfo.add(excelRowData);
        }
    }
    else if (store.getPosType().equals(ExtStoreImpl.POS_TYPE_INFOR))
    {
        for(OrderHistoryBean orderHistory : ohs)
        {
            excelRowData.put("Order Number", orderHistory.getOrderNumber());
            excelRowData.put("PO Number", orderHistory.getPoNumber());
            excelRowData.put("Order Date", orderHistory.getPostedDate());
            excelRowData.put("Status", orderHistory.getStatus());
            excelRowData.put("Order Total", orderHistory.getAmount());

            productInfo.add(excelRowData);
        }
    }
    else if (store.getPosType().equals(ExtStoreImpl.POS_TYPE_NRT))
    {
        for(OrderHistoryBean orderHistory : ohs)
        {
            excelRowData.put("Order Number", orderHistory.getOrderNumber());
            excelRowData.put("Store", store);
            excelRowData.put("Order Date", orderHistory.getPostedDate());
            excelRowData.put("Order Total", orderHistory.getAmount());

            productInfo.add(excelRowData);
        }
    }   
    return productInfo; 
}

另外,基本相同的代码在我的Groovy类中运行得很好:

private ModelAndView performHistorySearch(final HttpServletRequest request){
    JsCustomerSession sess = (JsCustomerSession) getRequestHelper().getCustomerSession(request);
    JsCustomer customer = sess.getShopper().getCustomer();
    ExtStore store = (ExtStore) sess.getShopper().getCurrentShoppingCart().getStore();
    RemoteOrderHistoryService orderHistoryService = remoteServiceLocator.getOrderHistoryService(store);
    Map<String, Object> model = new HashMap<String, Object>();

    DateFormat formatter = new SimpleDateFormat("MM-dd-yyyy");
    Calendar cal = Calendar.getInstance();
    String defaultEnd = formatter.format(cal.getTime());
    cal.add(Calendar.DATE, -30);

    String defaultStart = formatter.format(cal.getTime());
    String startStr = ServletRequestUtils.getStringParameter(request, "start");
    Date end = formatter.parse( ServletRequestUtils.getStringParameter(request, "end", defaultEnd) );
    String searchString = ServletRequestUtils.getStringParameter(request, "searchString", "").trim();
    String show = ServletRequestUtils.getStringParameter(request, "show", "");
    String searchType = ServletRequestUtils.getStringParameter(request, "search-filter", "");

    if(startStr == null) {
        if(searchString != "") {
            Calendar newStart = Calendar.getInstance();
            newStart.add(Calendar.YEAR, -25);
            startStr = formatter.format(newStart.getTime());
        } else {
            startStr = defaultStart;
        }   
    }

    if (show == ""){
        show = 'all';
    }

    if(store.getPosType().equals(ExtStoreImpl.POS_TYPE_INFOR) || store.getPosType().equals(ExtStoreImpl.POS_TYPE_DATABASE)){
        searchString  += 'show: ' + show + "type: " + searchType;
    }

    Date start = formatter.parse( startStr );

    log.debug("SEARCHING FROM " + start + ' TO ' + end);
    log.debug("SEARCHING FOR " + searchString);
    log.debug("SEARCHING TYPE " + searchType);

    List<OrderHistoryBean> ohs = orderHistoryService.getOrderHistory(customer, start, end, searchString);

    if (store.getPosType().equals(ExtStoreImpl.POS_TYPE_INFOR) || store.getPosType().equals(ExtStoreImpl.POS_TYPE_DATABASE)){
        model.put("orders", ohs);
    } else {
        List<OrderHistoryBean> returnOhs = new ArrayList<OrderHistoryBean>();
        ohs.each({oh -> 
            if(oh.getType() != 'Payment') {
                if(show == 'all') {
                    returnOhs.add(oh);
                    log.debug('adding ' + oh.getOrderNumber());
                } else if (show == 'invoice' && oh.getType() == 'Invoice') {
                    returnOhs.add(oh);
                    log.debug('adding ' + oh.getOrderNumber());
                } else if (show == 'open' && oh.getType() != 'Invoice') {
                    returnOhs.add(oh);
                    log.debug('adding ' + oh.getOrderNumber());
                }
            }
        })
        log.debug(returnOhs.size());
        model.put("orders", returnOhs);
    }

    return new ModelAndView(this.getSuccessView(), model);
}

我被困住了,任何帮助都会受到赞赏。

1 个答案:

答案 0 :(得分:0)

试试这个:

Date end;
try {
   end = formatter.parse(endStr);
} catch (ParseException e) {
   e.printStackTrace();
   throw e;
}

Date end;
try {
   end = formatter.parse(endStr);
} catch (ParseException e) {
   e.printStackTrace();
   end = new Date(); // default to now
}