Error formatting date:

2

Hello, I am formatting the date with the following code:

Date data = null;

    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    try {

      data = format.parse(request.getParameter("txtdata"));

    } catch (Exception e) {

        JOptionPane.showMessageDialog(null,"Erro ao formatar data: " +e.getMessage());

    }
    event.setEventodata(data);

By clicking on register, in the form below:

Thefollowingerrorappears:

I'm saving this in the bank ok? The attribute type is: datetime not null ; Is there anything wrong with the conversion?

    
asked by anonymous 27.05.2016 / 22:44

2 answers

2

Good afternoon friend, please post your Model Class.

Following is an example of formatting with SimpleDateFormat ....

SimpleDateFormat fmt = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
Date data = fmt.parse("17/12/2007 19:30:20"); 
String str = fmt.format(data); 
    
21.06.2018 / 21:35
1

From the error message we can have a clue about the problem:

  

Unparseable date: "2016-07-28 T 21:58:58"

Note that between the date ( 2016-07-28 ) and time ( 21:58:58 ) there is a T letter. This letter is the separator that must be between the date and time, according to the format defined by ISO 8601 (and which appears to be the format in which the parameter is being sent.)

The SimpleDateFormat gave error because you are using a space between the date ( yyyy-MM-dd ) and the time ( HH:mm:ss ). To correct, just change the space with the letter T .

A detail is that it must be enclosed in single quotation marks (% with%), so that 'T' understands that it is the letter T itself (otherwise it will try to interpret it as a letter with a special meaning, such as the SimpleDateFormat that means "year", for example).

Then it would look like this:

SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");

With this, y will be done correctly.

    
21.06.2018 / 20:39