0

I have an issue with inserting datatime from the form input into the database. Specifically, I managed to parse data, but not the time.

I have database with DATETIME column 'timeFinished'.

I have JSP input:

<input type="datetime-local" path="timeFinished" name="timeFinished" id="timeFinished"/>

I have controller with InitBinder:

@InitBinder
public void initBinder(WebDataBinder binder) {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    sdf.setLenient(true);
    binder.registerCustomEditor(Date.class, new CustomDateEditor(sdf, true));
}

In this configuration the date is successfully being parsed to database like this:

1989-12-31 22:00:00

I don't know where is he getting 22:00:00, but it is like this. But I need to parse the time too. And it is not working when I add time to pattern:

    @InitBinder
    public void initBinder(WebDataBinder binder) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm");
        sdf.setLenient(true);
        binder.registerCustomEditor(Date.class, new CustomDateEditor(sdf, true));
    }
}

In this case the datetime value become NULL and nothing is being parsed to database at all. No errors are being showed.

How I can manage to parse both date and time into database? I've tried all possibilities for patterns - hh:mm:ss, HH:MM, HH:MM:SS and others. If I add time - it is not working. It works if I put date only.

Thank you in advance for your help.

mrrunawayz
  • 49
  • 1
  • 6
  • Thank you for your link, but it is still not working with this: `SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");` – mrrunawayz Jul 15 '18 at 08:00
  • `@InitBinder public void initBinder(WebDataBinder binder) { binder.registerCustomEditor( String.class, new StringTrimmerEditor( true )); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); sdf.setLenient(false); binder.registerCustomEditor(Date.class, new CustomDateEditor(sdf, false)); }` still not working – mrrunawayz Jul 15 '18 at 08:25
  • I recommend you avoid the `SimpleDateFormat` class. It is not only long outdated, it is also notoriously troublesome. Today we have so much better in [`java.time`, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. Jul 15 '18 at 14:16
  • `22:00:00` sounds like a time zone issue, said without knowing the details. – Ole V.V. Jul 15 '18 at 14:20

1 Answers1

0

Finally the problem is solved. Solution:

Controller - add this pattern to SimpleDateFormat:

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm");

JSP - add this to input:

pattern="yyyy-MM-dd'T'hh:mm:ss"
mrrunawayz
  • 49
  • 1
  • 6
  • Please test this with an hour of day of 12, for example 2018-07-16T12:15. Asking because I suspect that lowercase `hh` in the format pattern strings will give you a wrong result in this case. – Ole V.V. Jul 16 '18 at 08:58
  • Yes, you are right. With lowercase `hh` I get 12 hours format. Changed to `HH`. Thank you. – mrrunawayz Jul 16 '18 at 11:30