我需要帮助将LocalDate
解析为HashMap
代币。
currentOrder.setOrderDate(currentTokens[12])
。 orderDate
对象中的Order
被赋予LocalDate
类型。给出了错误:
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();
}
订单对象;
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(){
}
答案 0 :(得分:1)
您正在从文件中提取字符串并尝试将其添加到LocalDate。根据以下java文档,您应该使用parse方法将String日期解析为LocalDate对象。
String myDate = "2007-12-03";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
LocalDate date = LocalDate.parse(myDate, formatter);