2017-09-23 68 views
0

我需要幫助解析LocalDateHashMap令牌。 currentOrder.setOrderDate(currentTokens[12])。在Order對象中的orderDate被給予LocalDate類型。被賦予了錯誤:解析LocalDate hashMap令牌

incompatible types: String cannot be converted to LocalDate. 

我需要一種方法來解析它在

private void loadOrder(LocalDate date) throws OrderPersistenceException { 
    String path = getFilePath(date); 
    File f = new File(path); 
    if (!f.exists()) { 
     return; 
    } 

    Scanner scanner = null; 

    try { 
     scanner = new Scanner(new BufferedReader(new FileReader(path))); 
    } catch (FileNotFoundException ex) { 
     throw new OrderPersistenceException(""); 
    } 
    String currentLine; 
    String[] currentTokens; 

    while (scanner.hasNextLine()) { 
     currentLine = scanner.nextLine(); 
     currentTokens = currentLine.split(DELIMITER); 
     HashMap<Long, Order> orders = new HashMap<>(); 
     Order currentOrder = new Order(Long.parseLong(currentTokens[0])); 
     currentOrder.setCustomerName(currentTokens[1]); 
     currentOrder.setState(currentTokens[2]); 
     currentOrder.setTaxRate(new BigDecimal(currentTokens[3])); 
     currentOrder.setProductType(currentTokens[4]); 
     currentOrder.setArea(new BigDecimal(currentTokens[5])); 
     currentOrder.setCostPerSquareFoot(new BigDecimal(currentTokens[6])); 
     currentOrder.setLaborCostPerSquareFoot(new BigDecimal(currentTokens[7])); 
     currentOrder.setMeterialCost(new BigDecimal(currentTokens[8])); 
     currentOrder.setLaborCost(new BigDecimal(currentTokens[9])); 
     currentOrder.setTotalTax(new BigDecimal(currentTokens[10])); 
     currentOrder.setTotal(new BigDecimal(currentTokens[11])); 
     currentOrder.setOrderDate(currentTokens[12]); 
     orders.put(currentOrder.getOrderNumber(), currentOrder); 

     if (!orderMap.containsKey(currentOrder.getOrderDate())) { 
      orderMap.put(currentOrder.getOrderDate(), orders); 
     } else { 
      orderMap.get(currentOrder.getOrderDate()).put(currentOrder.getOrderNumber(), currentOrder); 

     } 
    } 
    scanner.close(); 

} 

Order對象。

public class Order { 

    private Long orderNumber; 
    private String customerName; 
    private String state; 
    private BigDecimal taxRate; 
    private String productType; 
    private BigDecimal area; 
    private BigDecimal costPerSquareFoot; 
    private BigDecimal laborCostPerSquareFoot; 
    private BigDecimal meterialCost; 
    private BigDecimal laborCost; 
    private BigDecimal totalTax; 
    private BigDecimal total; 
    private LocalDate orderDate; 




public Order(){ 

} 

回答

1

您正在從文件中提取字符串並嘗試將其添加到LocalDate中。根據以下java文檔,您應該使用parse方法將String日期解析爲LocalDate對象。

https://docs.oracle.com/javase/8/docs/api/java/time/LocalDate.html#parse-java.lang.CharSequence-java.time.format.DateTimeFormatter-

String myDate = "2007-12-03"; 
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd"); 
    LocalDate date = LocalDate.parse(myDate, formatter); 
+0

無需爲'formatter'對象。該輸入的標準ISO 8601格式在java.time類中默認支持。 'LocalDate.parse(「2017-12-03」)' –