gpt4 book ai didi

java - 将 LocalDate 解析为 hashMap Token

转载 作者:行者123 更新时间:2023-12-02 12:13:06 26 4
gpt4 key购买 nike

我需要帮助将 LocalDate 解析为 HashMap token 。currentOrder.setOrderDate(currentTokens[12])Order 对象中的 orderDate 被赋予了 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(){

}

最佳答案

您正在从文件中提取字符串并尝试将其添加到 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);

关于java - 将 LocalDate 解析为 hashMap Token,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46385330/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com