我有这两部分代码。
添加到arraylist不起作用,f arrylist始终为空。在输出调试窗口中,我收到的唯一消息是每个Fattura的Unparseable date:“2014-12-09 00:00:00.0”。
private ArrayList<Fattura> getFatture() throws SQLException {
String localDb;
String unformattedQuery;
ArrayList<Fattura> ftt = new ArrayList<Fattura>();
if (this.serviceSelection.getSelectedItem() == "Energia") {
localDb = EmailCheckerGUI.DbEnergia;
unformattedQuery = ServiceQuery.RECUPERAFATTUREENERGIA.getQuery();
} else {
localDb = EmailCheckerGUI.DbGas;
unformattedQuery = ServiceQuery.RECUPERAFATTUREGAS.getQuery();
}
DbHandlerContext db = new DbHandlerContext(new SqlServerConcrete(EmailCheckerGUI.user, EmailCheckerGUI.pwd, localDb));
String formattedQuery = String.format(unformattedQuery,
BillIntervalContainer.getFormattedMinDataFatt("MM/dd/yyyy"),
BillIntervalContainer.getFormattedMaxDataFatt("MM/dd/yyyy"),
BillIntervalContainer.minFattNumber, BillIntervalContainer.maxFattNumber);
ResultSet r = db.executeQuery(formattedQuery);
while (r.next()) {
try {
boolean addingelement = false;
String ioError = "";
try {
addingelement = ftt.add(new Fattura(r.getInt("id"), r.getString("servizio"), r.getString("email"),
r.getString("denominazione"), r.getString("data_fattura"), r.getString("data_scadenza"), r.getString("path_fattura"),
r.getInt("inviato"), r.getString("report"), r.getInt("numero_fattura"), r.getInt("id_documento")));
} catch (IOException ex) {
Logger.getLogger(EmailCheckerGUI.class.getName()).log(Level.SEVERE, null, ex);
ioError = ex.getMessage();
}
if (!addingelement) {
if (!"".equals(ioError)) {
addToLog("Impossibile aggiungere alla lista di invii la fattura numero " + r.getString("numero_fattura"));
} else {
addToLog("Impossibile aggiungere alla lista di invii la fattura numero " + r.getString("numero_fattura") + ": " + ioError);
}
} else {
System.out.println(r.getString("numero_fattura"));
}
} catch (ParseException ex) {
System.out.println(ex.getMessage());
}
}
return ftt;
}
和
Fattura( int id, String service, String email,
String name, String dateBill, String dateScad, String path,
int sent, String report, int billNumber, int idDocument) throws ParseException, IOException{
this.id = id;
this.service = service;
this.email = email;
this.name = name;
this.path = path;
this.report = report;
this.sent = sent;
this.billNumber = billNumber;
this.dateBill = new SimpleDateFormat("yyyy-MM-dd").parse(dateBill);
this.dateScad = new SimpleDateFormat("yyyy-MM-dd").parse(dateScad);
this.idDocument = idDocument;
this.pdfDocument = new PdfValidator(null, this.name, dateBill, String.valueOf(this.billNumber), String.valueOf(0), this.path);
}
知道我哪里错了吗? 感谢
答案 0 :(得分:4)
您正在使用周年:YYYY而非年yyyy。请查看SimpleDateFormat documentation。您将找到所有模式和一些有用的示例。
现在yyyy-MM-dd
只会解析2014-12-09。如果你还有时间,你应该使用如下:
yyyy-MM-dd HH:mm:ss.S
答案 1 :(得分:0)
或者您可以使用.format
代替.parse
:
this.dateBill = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
但请注意MM
不能小写。