You are viewing a plain text version of this content. The canonical link for it is here.
Posted to taglibs-user@tomcat.apache.org by Kazuaki Miyauchi <mi...@gmail.com> on 2019/08/19 03:45:32 UTC

How to convert String to Date in EL?

 I made following JSP.

<%@ page contentType="text/html;charset=UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql" %>

<html>
<head>
  <title>Insert Date</title>
</head>
<body>

<c:catch var="ex">
<sql:setDataSource dataSource="jdbc/kome" var="kome"/>
<sql:update var="stmt" dataSource="${kome}">
 insert into test values(?,?)
 <sql:dateParam value="2019-8-19" type="DATE"/>
 <sql:param value="Good Day"/>
</sql:update>
</c:catch>

<c:if test="${not empty ex}">
 <c:out value="${ex}" />
</c:if>
</body>
</html>

Table was simply made as following.
create table test (when_cause date, how text);

 Then, I got following error in tomcat-9.0.22.
org.apache.jasper.JasperException: Unable to convert string
[2019-8-19] to class [java.util.Date] for attribute [value]: [Property
Editor not registered with the PropertyEditorManager]

 How to convert String date data to Date type?

 After changing as following, it works.
But, it's not easily readable when making complicated sql.

<sql:update var="stmt" dataSource="${kome}">
 insert into test values('2019-8-19',?)
 <sql:param value="Good Day"/>
</sql:update>

Regards,                         Kazuaki Miyauchi, Japan

---------------------------------------------------------------------
To unsubscribe, e-mail: taglibs-user-unsubscribe@tomcat.apache.org
For additional commands, e-mail: taglibs-user-help@tomcat.apache.org


Re: How to convert String to Date in EL?

Posted by Kazuaki Miyauchi <mi...@gmail.com>.
Excuse me. PC sent my under-writing mail.
2019年8月20日(火) 18:14 Kazuaki Miyauchi <mi...@gmail.com>:
>  I think many people use JavaBeans or like that using Date data.
> But, in Web site, year, month, day are mostly requested parameters.
> It was very convenient I can use ${param.year} and s

It was very convenient I can use ${param.year} and so on for DB access data.

 Regards,                          Kazuaki Miyauchi, Japan

---------------------------------------------------------------------
To unsubscribe, e-mail: taglibs-user-unsubscribe@tomcat.apache.org
For additional commands, e-mail: taglibs-user-help@tomcat.apache.org


Re: How to convert String to Date in EL?

Posted by Kazuaki Miyauchi <mi...@gmail.com>.
2019年8月20日(火) 17:55 Stuart Thiel <st...@gmail.com>:
> I suspect you can skip the set tag and wrap the parseDate in the sqlParam
> tag as well, but I've never mucked with sql in JSPs (I'm pretty strict
> about using them exclusively as Template Views)

 I think many people use JavaBeans or like that using Date data.
But, in Web site, year, month, day are mostly requested parameters.
It was very convenient I can use ${param.year} and s

---------------------------------------------------------------------
To unsubscribe, e-mail: taglibs-user-unsubscribe@tomcat.apache.org
For additional commands, e-mail: taglibs-user-help@tomcat.apache.org


Re: How to convert String to Date in EL?

Posted by Stuart Thiel <st...@gmail.com>.
I suspect you can skip the set tag and wrap the parseDate in the sqlParam
tag as well, but I've never mucked with sql in JSPs (I'm pretty strict
about using them exclusively as Template Views)

On Tue, Aug 20, 2019, 05:44 Stuart Thiel <st...@gmail.com> wrote:

> Because formats for dates are so ambiguous, there's no reliable way to
> interpret. The is why you'd use the parseDate tag, probably inside a set to
> get a date object that you'd then pass to your dateparam thing. Presumably
> you could write an el "function" to do that inline if you wanted to out in
> the effort to clean your JSP further, as I don't know of such thing
> existing (though I can't imagine why it shouldn't, save to discourage
> everyone putting all their application logic and validation in JSPs)
>
> On Tue, Aug 20, 2019, 05:22 Kazuaki Miyauchi <mi...@gmail.com> wrote:
>
>> 2019-08-20 16:39 GMT+09:00, Stuart Thiel <st...@gmail.com>:
>> > That's what parseDate does, and why I pointed you at it.
>>
>>  I also want to do like int case as following.
>>
>> <c:set var="year" value="2019" scope="session"/>
>> <c:set var="month" value="8" scope="session"/>
>> <c:set var="day" value="20" scope="session"/>
>>
>> <sql:setDataSource dataSource="jdbc/kome" var="kome"/>
>> <sql:update var="stmt" dataSource="${kome}">
>>  insert into test values(?,?)
>>  <sql:dateParam value="${year}-${month}-${day}" type="date"/>
>>  <sql:param value="Good"/>
>> </sql:update>
>>
>>  But, of course, this makes
>>
>> javax.el.ELException: Cannot convert [2019-8-20] of type [class
>> java.lang.String] to [class java.util.Date]
>>
>>  So, I'd like to know how to convert Date type using EL writing.
>> Is there simple method to convert like 0 + for int case?
>>
>>  Regards,                            Kazuaki Miyauchi, Japan
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: taglibs-user-unsubscribe@tomcat.apache.org
>> For additional commands, e-mail: taglibs-user-help@tomcat.apache.org
>>
>>

Re: How to convert String to Date in EL?

Posted by Kazuaki Miyauchi <mi...@gmail.com>.
2019年8月20日(火) 17:44 Stuart Thiel <st...@gmail.com>:
> Because formats for dates are so ambiguous, there's no reliable way to
> interpret.

<sql:dateParam2 value="2019/8/23" type="date" pattern="yyyy/MM/dd"/>

 isn't so ambiguous.

Regards,                                          Kazuaki Miyauchi, Japan

---------------------------------------------------------------------
To unsubscribe, e-mail: taglibs-user-unsubscribe@tomcat.apache.org
For additional commands, e-mail: taglibs-user-help@tomcat.apache.org


Re: How to convert String to Date in EL?

Posted by Stuart Thiel <st...@gmail.com>.
Because formats for dates are so ambiguous, there's no reliable way to
interpret. The is why you'd use the parseDate tag, probably inside a set to
get a date object that you'd then pass to your dateparam thing. Presumably
you could write an el "function" to do that inline if you wanted to out in
the effort to clean your JSP further, as I don't know of such thing
existing (though I can't imagine why it shouldn't, save to discourage
everyone putting all their application logic and validation in JSPs)

On Tue, Aug 20, 2019, 05:22 Kazuaki Miyauchi <mi...@gmail.com> wrote:

> 2019-08-20 16:39 GMT+09:00, Stuart Thiel <st...@gmail.com>:
> > That's what parseDate does, and why I pointed you at it.
>
>  I also want to do like int case as following.
>
> <c:set var="year" value="2019" scope="session"/>
> <c:set var="month" value="8" scope="session"/>
> <c:set var="day" value="20" scope="session"/>
>
> <sql:setDataSource dataSource="jdbc/kome" var="kome"/>
> <sql:update var="stmt" dataSource="${kome}">
>  insert into test values(?,?)
>  <sql:dateParam value="${year}-${month}-${day}" type="date"/>
>  <sql:param value="Good"/>
> </sql:update>
>
>  But, of course, this makes
>
> javax.el.ELException: Cannot convert [2019-8-20] of type [class
> java.lang.String] to [class java.util.Date]
>
>  So, I'd like to know how to convert Date type using EL writing.
> Is there simple method to convert like 0 + for int case?
>
>  Regards,                            Kazuaki Miyauchi, Japan
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: taglibs-user-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: taglibs-user-help@tomcat.apache.org
>
>

Re: How to convert String to Date in EL?

Posted by Kazuaki Miyauchi <mi...@gmail.com>.
2019-08-20 16:39 GMT+09:00, Stuart Thiel <st...@gmail.com>:
> That's what parseDate does, and why I pointed you at it.

 I also want to do like int case as following.

<c:set var="year" value="2019" scope="session"/>
<c:set var="month" value="8" scope="session"/>
<c:set var="day" value="20" scope="session"/>

<sql:setDataSource dataSource="jdbc/kome" var="kome"/>
<sql:update var="stmt" dataSource="${kome}">
 insert into test values(?,?)
 <sql:dateParam value="${year}-${month}-${day}" type="date"/>
 <sql:param value="Good"/>
</sql:update>

 But, of course, this makes

javax.el.ELException: Cannot convert [2019-8-20] of type [class
java.lang.String] to [class java.util.Date]

 So, I'd like to know how to convert Date type using EL writing.
Is there simple method to convert like 0 + for int case?

 Regards,                            Kazuaki Miyauchi, Japan

---------------------------------------------------------------------
To unsubscribe, e-mail: taglibs-user-unsubscribe@tomcat.apache.org
For additional commands, e-mail: taglibs-user-help@tomcat.apache.org


Re: How to convert String to Date in EL?

Posted by Stuart Thiel <st...@gmail.com>.
That's what parseDate does, and why I pointed you at it.

On Tue, Aug 20, 2019, 04:15 Kazuaki Miyauchi <mi...@gmail.com> wrote:

>  This also works using Java description as following.
>
> <c:set var="date" value="2019/8/20" scope="session"/>
> <% SimpleDateFormat sdFormat = new SimpleDateFormat("yyyy/MM/dd");
>    Date date = sdFormat.parse(session.getAttribute("date").toString());
>    session.setAttribute("date_o", date); %>
> <sql:setDataSource dataSource="jdbc/kome" var="kome"/>
> <sql:update var="stmt" dataSource="${kome}">
>  insert into test values(?,?)
>  <sql:dateParam value="${date_o}" type="date"/>
>  <sql:param value="Good"/>
> </sql:update>
>
>  We need such complicated way to Web-DB access?
> We need simple way using EL description.
>
>  Regards,                            Kazuaki Miyauchi, Japan
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: taglibs-user-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: taglibs-user-help@tomcat.apache.org
>
>

Re: How to convert String to Date in EL?

Posted by Kazuaki Miyauchi <mi...@gmail.com>.
 This also works using Java description as following.

<c:set var="date" value="2019/8/20" scope="session"/>
<% SimpleDateFormat sdFormat = new SimpleDateFormat("yyyy/MM/dd");
   Date date = sdFormat.parse(session.getAttribute("date").toString());
   session.setAttribute("date_o", date); %>
<sql:setDataSource dataSource="jdbc/kome" var="kome"/>
<sql:update var="stmt" dataSource="${kome}">
 insert into test values(?,?)
 <sql:dateParam value="${date_o}" type="date"/>
 <sql:param value="Good"/>
</sql:update>

 We need such complicated way to Web-DB access?
We need simple way using EL description.

 Regards,                            Kazuaki Miyauchi, Japan

---------------------------------------------------------------------
To unsubscribe, e-mail: taglibs-user-unsubscribe@tomcat.apache.org
For additional commands, e-mail: taglibs-user-help@tomcat.apache.org


Re: How to convert String to Date in EL?

Posted by Stuart Thiel <st...@gmail.com>.
Nope, its to convert a string to a date. I think BalusC is the answer
(ranked 246 or something). While the initial question was not what you were
looking for, the reason I suggested you have a read is because you can
often learn a lot of other things (in particular what you are looking for),
and I'm suspecting you didn't take that advice here.

On Mon, Aug 19, 2019, 01:30 Kazuaki Miyauchi <mi...@gmail.com> wrote:

>  Hi, Stuart, Thanks for your quick reply.
>
> 2019年8月19日(月) 13:01 Stuart Thiel <st...@gmail.com>:
> > The solution with fmt:parseDate is likely what you want, but read what
> > they say and perhaps change what you're doing a bit.
>
>  That is to convert Date type to String.
> I'd like to know reverse way. Google doesn't hit this error message.
>
> Regards,                                        Kazuaki Miyauchi, Japan
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: taglibs-user-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: taglibs-user-help@tomcat.apache.org
>
>

Re: How to convert String to Date in EL?

Posted by Kazuaki Miyauchi <mi...@gmail.com>.
 Hi, Stuart, Thanks for your quick reply.

2019年8月19日(月) 13:01 Stuart Thiel <st...@gmail.com>:
> The solution with fmt:parseDate is likely what you want, but read what
> they say and perhaps change what you're doing a bit.

 That is to convert Date type to String.
I'd like to know reverse way. Google doesn't hit this error message.

Regards,                                        Kazuaki Miyauchi, Japan

---------------------------------------------------------------------
To unsubscribe, e-mail: taglibs-user-unsubscribe@tomcat.apache.org
For additional commands, e-mail: taglibs-user-help@tomcat.apache.org


Re: How to convert String to Date in EL?

Posted by Stuart Thiel <st...@gmail.com>.
Hello Kazuaki,

A quick google is probably more effective than mailing this list every time:
https://stackoverflow.com/questions/6162401/convert-and-format-a-date-in-jsp

The solution with fmt:parseDate is likely what you want, but read what
they say and perhaps change what you're doing a bit.

On Sun, Aug 18, 2019 at 11:46 PM Kazuaki Miyauchi <mi...@gmail.com> wrote:
>
>  I made following JSP.
>
> <%@ page contentType="text/html;charset=UTF-8"%>
> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
> <%@ taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql" %>
>
> <html>
> <head>
>   <title>Insert Date</title>
> </head>
> <body>
>
> <c:catch var="ex">
> <sql:setDataSource dataSource="jdbc/kome" var="kome"/>
> <sql:update var="stmt" dataSource="${kome}">
>  insert into test values(?,?)
>  <sql:dateParam value="2019-8-19" type="DATE"/>
>  <sql:param value="Good Day"/>
> </sql:update>
> </c:catch>
>
> <c:if test="${not empty ex}">
>  <c:out value="${ex}" />
> </c:if>
> </body>
> </html>
>
> Table was simply made as following.
> create table test (when_cause date, how text);
>
>  Then, I got following error in tomcat-9.0.22.
> org.apache.jasper.JasperException: Unable to convert string
> [2019-8-19] to class [java.util.Date] for attribute [value]: [Property
> Editor not registered with the PropertyEditorManager]
>
>  How to convert String date data to Date type?
>
>  After changing as following, it works.
> But, it's not easily readable when making complicated sql.
>
> <sql:update var="stmt" dataSource="${kome}">
>  insert into test values('2019-8-19',?)
>  <sql:param value="Good Day"/>
> </sql:update>
>
> Regards,                         Kazuaki Miyauchi, Japan
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: taglibs-user-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: taglibs-user-help@tomcat.apache.org
>


-- 
Stuart Thiel, P. Eng.

---------------------------------------------------------------------
To unsubscribe, e-mail: taglibs-user-unsubscribe@tomcat.apache.org
For additional commands, e-mail: taglibs-user-help@tomcat.apache.org