-1

i have these two parts of code.

Adding to the arraylist doesn't work, f arrylist is always empty. In the output debug window, the only message that i receive is Unparseable date: "2014-12-09 00:00:00.0" for each Fattura.

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;
}

and

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);

}

Any idea of where i'm wrong? Thanks

  • 1
    Please try to provide a *minimal* example. You could have reduced this much further. – Thomas Weller Dec 15 '14 at 14:42
  • possible duplicate of [Error when converting date format to another](http://stackoverflow.com/questions/27202043/error-when-converting-date-format-to-another) – Basil Bourque Dec 16 '14 at 22:39

2 Answers2

4

You are using week year : YYYY instead of year yyyy. Please take a look at the SimpleDateFormat documentation. You'll find all the patterns and some usefull examples.

Now yyyy-MM-dd will only parse 2014-12-09. If you have also time after date you should use something like : yyyy-MM-dd HH:mm:ss.S

alain.janinm
  • 19,951
  • 10
  • 65
  • 112
  • Sorry it's a copy/paste mistake. I really use the yyyy format, i was just tryin'. It doesn't work even in the correct format. Thanks for the answer – Leandro Luccerini Dec 15 '14 at 14:56
  • Thanks again! I've already tried it but still not working, same problem – Leandro Luccerini Dec 15 '14 at 15:27
  • @LeandroLuccerini If you run `new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S").parse("2014-12-09 00:00:00.0")` in a main it will work.So if you still get exception it's either because you didn't correct the pattern everywhere or your date format is not always like `2014-12-09 00:00:00.0` – alain.janinm Dec 15 '14 at 15:36
0

Or You can use .format instead of .parse :

this.dateBill  = new SimpleDateFormat("yyyy-MM-dd").format(new Date());

but watch out that MM can't be lowercase.

Fevly Pallar
  • 3,059
  • 2
  • 15
  • 19