0
我需要幫助解析LocalDate
到HashMap
令牌。 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(){
}
無需爲'formatter'對象。該輸入的標準ISO 8601格式在java.time類中默認支持。 'LocalDate.parse(「2017-12-03」)' –