-1

I want to parse a String into a SQL DateTime. The string is the result of the concatenation of a SQL Date and a String that contains the time with the following format: HH:MM.

My code:

try {
        java.util.Date date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(beanActividad.getFecha().toString() + " " + beanActividad.getHora() + ":00");
        datetime = new java.sql.Timestamp(date.getTime());
        } catch (ParseException ex) {
            Logger.getLogger(ActividadesBackingBean.class.getName()).log(Level.SEVERE, null, ex);
        }

beanActividad.getHora() gets the hour String part, beanActividad.getFecha().toString() the java util Date part and finally it all concatenates with ":00", so it has a SQL DateTime properly syntax.

The error:

java.text.ParseException: Unparseable date: "Tue May 26 00:00:00 CEST 2020 12:00:00"

What i want is to get a TimeStamp, so I can load it into the database as a SQL DateTime.

deadlock
  • 164
  • 1
  • 14

1 Answers1

0

You must define a formatting pattern to match your input precisely. "yyyy-MM-dd HH:mm:ss" is not even close to matching "Tue May 26 00:00:00 CEST 2020 12:00:00".

Tip: That input's format is a terrible choice for exchanging date-time values. I suggest you educate the publisher of your data regarding the ISO 8601 standard.

I cannot show a formatting pattern to match your input. Your input example has two time-of-day values, contradictory. So the input is senseless, from what I can gather.

Another problem: You are using terrible date-time classes that were supplanted years ago by the modern java.time classes defined in JSR 310.

All this has been covered many many times on Stack Overflow. Search thoroughly before posting.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154