所以这里是我一直试图转换的链接http://www2.hawaii.edu/~takebaya/ics111/jtable_basic/jtable_basic.html 。
2011-12-22,12:28:51,12:28:53
2012-10-22,12:28:57,12:28:59
2010-10-22,12:29:10,12:29:12
基本上我想要的是让我的 JTable 在此处显示我的 csv 文件的内容:
package SimpleBook;
import java.time.DateTimeException;
import java.time.LocalDate;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.io.*;
public class SimpleBookList extends DateTimeException {
private ArrayList<SimpleBook> bookList;
public SimpleBookList() {
super("");
bookList = new ArrayList<SimpleBook>();
}
public void add(SimpleBook sb) {
bookList.add(sb);
}
public void readFromCSV(String filename) {
File file = new File(filename);
FileReader reader = null;
try {
reader = new FileReader(file);
}
catch (FileNotFoundException e) {
e.printStackTrace();
System.exit(1);
}
BufferedReader infile = new BufferedReader(reader);
String line = "";
DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern("HH:mm:ss");
try {
boolean done = false;
while (!done) {
line = infile.readLine();
if (line == null) {
done = true;
}
else {
String[] tokens = line.split(",");
LocalDate date = LocalDate.parse(tokens[0], dateFormatter);
LocalTime clockIn = LocalTime.parse(tokens[0], timeFormatter);
LocalTime clockOut = LocalTime.parse(tokens[0], timeFormatter);
SimpleBook sb = new SimpleBook(date,clockIn,clockOut);
bookList.add(sb);
}
}
}
catch (Exception e) {
e.printStackTrace();
System.exit(1);
}
}
public Object[][] convert2Data() {
Object[][] data = new Object[bookList.size()][3];
for (int i = 0; i < bookList.size(); i++) {
data[i][0] = bookList.get(i).getDate();
data[i][1] = bookList.get(i).getClockIn();
data[i][2] = bookList.get(i).getClockOut();
}
return data;
}
}
错误信息:
java.time.format.DateTimeParseException: Text '2011-12-22' could not be parsed at index 2 at java.base/java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:2052)
at java.base/java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1954)
at java.base/java.time.LocalTime.parse(LocalTime.java:465)
at SimpleBook.SimpleBookList.readFromCSV(SimpleBookList.java:44)
at SimpleBook.BasicJTable.<init>(BasicJTable.java:25)
at SimpleBook.BasicJTable.main(BasicJTable.java:40)