0

I have a JLabel with a date written as a String and I want to convert it into a sql.Date, but when I try to convert it, it run an exception. The date value is passed from a server with RMI.

Anyone know what could be the problem?

This is the RMI code:

PrenotazioniVaccini infoPrenotazione = null;    
    try {
        infoPrenotazione = stub.getPrenotazioneVaccinazione(tf_Cf.getText());
    } catch (RemoteException ex) {
        ex.printStackTrace();
    }
    
    label_DataVaccino.setText(infoPrenotazione.getData());

This is the label code:

label_DataVaccino = new JLabel("");
label_DataVaccino.setBounds(0, 320, 400, 25);
label_DataVaccino.setHorizontalAlignment(JLabel.CENTER);
label_DataVaccino.setVerticalAlignment(JLabel.CENTER);
panel_AddVaccinatoGUI.add(label_DataVaccino);

This is where the Exception run:

private void registraVaccinato() {
   String data = label_DataVaccino.getText();
   Date dataVaccino = Date.valueOf(data);
   System.out.println(dataVaccino);
}

And this is the error:

Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException
    at java.sql/java.sql.Date.valueOf(Date.java:141)
    at centrivaccinali.AddVaccinatoGUI.registraVaccinato(AddVaccinatoGUI.java:294)
    at centrivaccinali.AddVaccinatoGUI$3.actionPerformed(AddVaccinatoGUI.java:282)
    at java.desktop/javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1967)
    ...

I've used this guide to convert a String into a sql.Date: https://www.javatpoint.com/java-sql-date

2
  • Why, specifically, are you wanting to use java.sql.Date instead of modern types, particularly java.time.LocalDate? Commented Jul 30, 2021 at 18:24
  • @chrylis-cautiouslyoptimistic- Because I obtain the String type from a calendar created with jDatePicker.jar and I need to register the choosen date into a DB Commented Jul 30, 2021 at 18:26

1 Answer 1

2

IllegalArgumentException is when the argument is illegal or not proper.

The syntax for the data string needs to be in this format: yyyy-mm-dd. There are further restrictions like mm < 1 and mm > 12.

Sign up to request clarification or add additional context in comments.

2 Comments

I'm such an idiot, I've wrote the date in the format yyyy/mm/dd but it didn't works then watching your answer I've noticed that the date you wrote is in the format yyyy-mm-dd.
@Nico FYI, the YYYY-MM-DD format complies with the ISO 8601 standard. Stick to those standard formats when serializing textual date-time values, that is purpose of the standard. The java.time classes use these standard formats by default when parsing/generating strings.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.