java - 从文件读取的字符串未解析为本地日期

标签 java netbeans

public class Loan {

 private Borrower Borrower;
 private Book Book;
 private LocalDate issueDate;
 private LocalDate returnDate;
 private int status;

 public Loan(Borrower Borrower, Book Book, LocalDate issueDate, LocalDate returnDate, int status) {
    this.Borrower = Borrower;
    this.Book = Book;
    this.issueDate = issueDate;
    this.returnDate = returnDate;
    this.status = status;
 }

 public LocalDate getIssueDate() {
    return issueDate;
 }

 public void setIssueDate(LocalDate issueDate) {
    this.issueDate = issueDate;
 }

 public LocalDate getReturnDate() {
    return returnDate;
 }

 public void setReturnDate(LocalDate returnDate) {
    this.returnDate = returnDate;
 }

 public int getStatus() {
    return status;
 }

 public void setStatus(int status) {
    this.status = status;
 }

 public Borrower getBorrower() {
    return Borrower;
 }

 public void setBorrower(Borrower Borrower) {
    this.Borrower = Borrower;
 }

 public Book getBook() {
    return Book;
 }

 public void setBook(Book Book) {
    this.Book = Book;
 }

}    

ArrayList<Loan> Loans= new ArrayList();

try {
    BufferedReader reader = new BufferedReader(new FileReader("C:\\Users\\NimraArif\\Documents\\NetBeansProjects\\ooad1\\src\\ooad1\\Loans.txt"));
    String line = null;

    int bookid, status, borrowerid;

    line = reader.readLine();

    while (line!= null) {

      String[] values = line.split(",");

      bookid = Integer.valueOf(values[0]);
      borrowerid=Integer.valueOf(values[1]);
      DateTimeFormatter formatter= DateTimeFormatter.ofPattern("dd/MM/yy");
      LocalDate issueDate=LocalDate.parse(values[2],formatter);
      LocalDate returnDate=LocalDate.parse(values[3],formatter);
      status=Integer.valueOf(values[4]);
      Book temp = null; 
      Borrower temp2 = null;

      for (int i = 0; i < Books.size(); i++) {
        if (Books.get(i).getBookid()==bookid) {
          temp=Books.get(i);
        }
      }

      for (int i = 0; i < borrowers.size(); i++) {
        if (borrowers.get(i).getRollno()==borrowerid) {
          temp2=borrowers.get(i);
        }
      }

      Loan temp3 = new Loan(temp2, temp, issueDate, returnDate, status);
      Loans.add(temp3);
      line = reader.readLine();
    }
    reader.close();

  }
  catch (Exception e) {
    System.err.format("Exception occurred trying to read '%s'.", "Loans.txt");
    e.printStackTrace();
  }

当我执行此代码时,它在包含解析函数的行处引发异常。但是,如果我自己对字符串进行硬编码,则效果很好。我不明白为什么会发生这种情况。

这是从e.printStackTrace();获得的输出:

'Loans.txt'.java.time.format.DateTimeParseException: Text '2017/11/01' could not be parsed at index 2
        at java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:1949)
        at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1851)
        at java.time.LocalDate.parse(LocalDate.java:400)
        at ooad1.Ooad1.main(Ooad1.java:119)
Exception occurred trying to read 'Holder.txt'.

最佳答案

String read from a file is not getting parsed to localdate

LocalDate 是一个不可变类,表示日期,默认格式为 yyyy-MM-dd。我们可以使用 now() 方法来获取当前日期。我们还可以提供年、月和日期的输入参数来创建 LocalDate 实例。

使用格式化程序来解析 java.time.LocalDate 的单字符日/月字段

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd");
LocalDate ldate = LocalDate.parse("2017/11/01", formatter);

然后你可以使用LocalDate为您setter方法,之后就不会出现异常了。

例如

setIssueDate(ldate);
setReturnDate(ldate);

关于java - 从文件读取的字符串未解析为本地日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46731837/

相关文章:

eclipse - 将 java 6 maven webapp 从 netbeans 发布到 tomcat 时出现轻微的 url 不匹配

java - 在 Netbeans 7 中创建和存储 XML 文档

java - 从java类中的spring文件访问属性

java - 让 JSON 弹出窗口保存或打开对话框而不是实际内容

java - 关于用于压缩文件的免费库的建议

java - Glassfish 4 需要很长时间才能部署

javascript - TestCafe - Narbeans Ide - ReferenceError : Selector is not defined

Java opencv - 错误 - java.library.path 中没有 jniopencv_core

java - 在 Java 中使用 Integer 作为 HashMap 的键

java - 如何使用 Java HTTP 搜索数据?