0

I have a form with input of type "datetime-local" on a jsp page, the data is passed to a servlet:

String resetTimeString = request.getParameter(RequestParameterName.RESET_TIME);

How to convert the input to java.sql.Timestamp?

2 Answers 2

3

EDIT: Well, I found something new!

You can use Timestamp.valueOf() to format a time-string with the value of yyyy-[m]m-[d]d hh:mm:ss[.f...] So it can also handle micro/nano seconds. The only thing you need to do is replace the T with a space.

This works:

String datetimeLocal = "1985-04-12T23:20:50.52";
System.out.println(Timestamp.valueOf(datetimeLocal.replace("T"," ")));

The output:

1985-04-12 23:20:50.52

According to this site your resetTimeString looks like this: '1985-04-12T23:20:50.52' (a string)

I couldn't find a method to convert this to a timestamp directly, but you could just split it up manually:

String[] dateTime = datetimeLocal.split("T");
String[] date = dateTime[0].split("-");
String[] time = dateTime[1].split(":");

This will print:

System.out.println("DateTime: " + Arrays.toString(dateTime));
System.out.println("Date: " + Arrays.toString(date));
System.out.println("Time: " + Arrays.toString(time));

>>> DateTime: [1985-04-12, 23:20:50]
>>> Date: [1985, 04, 12]
>>> Time: [23, 20, 50]

After that you could just create a new Timestamp: (This is deprecated!)

Timestamp stamp = new Timestamp(Integer.valueOf(date[0]).intValue() - 1900, 
            Integer.valueOf(date[1]).intValue(), 
            Integer.valueOf(date[2]).intValue(), 
            Integer.valueOf(time[0]).intValue(), 
            Integer.valueOf(time[1]).intValue(), 
            Integer.valueOf(time[2].split("\\.")[0]).intValue(), 
            Integer.valueOf(time[2].split("\\.")[1]).intValue());

Note that, if you use this you need to subtract '1900' from the year and split dots with \\.

Also, you'd need to handle nanoseconds (In my example I'm using the value 50.52 as seconds, but the string returned from your server might not contain the nanoseconds)

You could also calculate a long from the date and use new Timestamp(<long>)

I hope this helps :)

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

3 Comments

I have hoped that there could be another, more convenient way to do it. But thank's anyway :)
Look at the top of my post ... I think that will help you better :)
The variant is really better. Thanks!
2

Cyphrags' answer won't work if seconds are set to "00", because Chrome won't send the seconds part resulting in a java.lang.IllegalArgumentException: Timestamp format must be yyyy-mm-dd hh:mm:ss[.fffffffff] when calling Timestamp.valueOf().

Therefore a more complete answer could be:

String datetimeLocal = "1985-04-12T23:20";
// make sure the seconds are set before parsing
if (StringUtils.countMatches(datetimelocal, ":") == 1) {
    datetimelocal += ":00";
}
Timestamp value = Timestamp.valueOf(datetimeLocal.replace("T", " "));

1 Comment

Welcome to SO! I took the liberty to remove your preamble because it could make someone feel that yours isn't an actual answer (therefore causing its deletion), while I think it is. Feel free to reject/edit differently

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.