| | |
how to convert string values to date and compare them while using an array
Please support our JSP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Thread Solved |
•
•
Join Date: Jul 2008
Posts: 50
Reputation:
Solved Threads: 0
hello everyone,
I am stuck with this problem.If anyone could help me, it would be a great favour.
I am working on a leave application.
Here the leaves are been applied by employees and accordingly the leaves are been approved or cancelled.
so the problem is that when i am retrieving values such as from date and to date from another jsp i am using
so that i get all the values of different employees.but when i am comparing it with the database values i am facing an error such as ,
only the dates which are from 12 to 30/31 .rest all the dates from 1 to 11 are not been updated in the database.i am just giving the code where the problem exists.I know how to do the connectivity and everything is working fine except for this one.
here i am using
Pls help me
Thanks in advance.
I am stuck with this problem.If anyone could help me, it would be a great favour.
I am working on a leave application.
Here the leaves are been applied by employees and accordingly the leaves are been approved or cancelled.
so the problem is that when i am retrieving values such as from date and to date from another jsp i am using
String[] leavefrom;
String[] until;
leavefrom=request.getParameterValues("leavefrom");
for(int counter=0; counter < leavefrom.length; counter++)
{
}
until=request.getParameterValues("until");
for(int counter=0; counter < until.length; counter++)
{
}so that i get all the values of different employees.but when i am comparing it with the database values i am facing an error such as ,
only the dates which are from 12 to 30/31 .rest all the dates from 1 to 11 are not been updated in the database.i am just giving the code where the problem exists.I know how to do the connectivity and everything is working fine except for this one.
ps=con.prepareStatement("update emp_leave_application set status='"+approved[counter]+"' where approver='"+username+"' and m_emp_no='"+userid[counter]+"' and leave_type='"+leavetype[counter]+"'and from_date=? and to_date=?");
ps.setString(1,leavefrom[counter]);
ps.setString(2,until[counter]);
ps.executeUpdate();ps.setString() wherein, i have to use ps.setDate() instead to compare it with the database, but how to write it when i am using an array .It is giving me an error saying that cant convert string to date. Pls help me
Thanks in advance.
First of all what type of argument does
Also if you need to convert a String to a Date try this:
Check the API for more: SimpleDateFormat
ps.setDate() have and what type are the to_date, from_date at your database?Also if you need to convert a String to a Date try this:
JSP Syntax (Toggle Plain Text)
String sDate = 01/04/2009; // 1st of April String format = "dd/MM/yyyy" SimpleDateFormat sdf = new SimpleDateFormat(format); sdf.setLenient(false); Date date = sdf.parse(sDate);
Check the API for more: SimpleDateFormat
Check out my New Bike at my Public Profile at the "About Me" tab
•
•
Join Date: Jul 2008
Posts: 50
Reputation:
Solved Threads: 0
•
•
•
•
First of all what type of argument doesps.setDate()have and what type are the to_date, from_date at your database?
Also if you need to convert a String to a Date try this:
JSP Syntax (Toggle Plain Text)
String sDate = 01/04/2009; // 1st of April String format = "dd/MM/yyyy" SimpleDateFormat sdf = new SimpleDateFormat(format); sdf.setLenient(false); Date date = sdf.parse(sDate);
Check the API for more: SimpleDateFormat
hi,
Thanks for your reply.
the code which i have written is given above in the previous post
i.e
JSP Syntax (Toggle Plain Text)
ps.setString(1,leavefrom[counter]); ps.setString(2,until[counter]);
and i am comparing this with the database which is in
JSP Syntax (Toggle Plain Text)
Date type i.e to_date, from_date
ps.setDate instead of ps.setString to compare it with the database and update all the values properly and i dont know what arguments i should pass in ps.setDate().because i tried using JSP Syntax (Toggle Plain Text)
ps.setDate(1,leavefrom[counter]); ps.setDate(2,until[counter]);
If there is a problem to understand the query then pls let me know.I will clarify it.It is very urgent and important for me.
Pls find a solution.
Thanks in advance.
•
•
Join Date: Nov 2007
Posts: 10
Reputation:
Solved Threads: 4
Hello,
Just remember that SimpleDateFormat.parse(String source) returns java.util.Date
and PreparedStatement.setDate(int i, Date d) takes java.sql.Date .
So, while passing the date parameter here, casting is required.
2nd thing keep in mind that while converting a String to Date - take care of format according to your database. Some may take as mm/dd/yyyy or dd/mm/yyyy. So, accordingly pass the parameter to SimpleDateFormat constructor.
Hope it solves your problem. And do let me know.
Just remember that SimpleDateFormat.parse(String source) returns java.util.Date
and PreparedStatement.setDate(int i, Date d) takes java.sql.Date .
So, while passing the date parameter here, casting is required.
2nd thing keep in mind that while converting a String to Date - take care of format according to your database. Some may take as mm/dd/yyyy or dd/mm/yyyy. So, accordingly pass the parameter to SimpleDateFormat constructor.
Hope it solves your problem. And do let me know.
•
•
•
•
2nd thing keep in mind that while converting a String to Date - take care of format according to your database. Some may take as mm/dd/yyyy or dd/mm/yyyy. So, accordingly pass the parameter to SimpleDateFormat constructor.
Hope it solves your problem. And do let me know.
Check for these sql functions:
to_date(), to_char()
Check out my New Bike at my Public Profile at the "About Me" tab
•
•
Join Date: Nov 2007
Posts: 10
Reputation:
Solved Threads: 4
•
•
•
•
If the column in the DB is declared to be type Date, the format is irrelevant. Because the column will contain Date not a Varchar. The data int the column doesn't have a format nor it needs one because it is type data. You can put data inside it in any way you want as long it is type Date.
Check for these sql functions:
to_date(), to_char()
Thanks
Last edited by deepalihanand; May 4th, 2009 at 4:07 pm.
•
•
Join Date: Jul 2008
Posts: 50
Reputation:
Solved Threads: 0
hi,
Thanks for the reply.
I do understand that SimpleDateFormat.parse(String source) returns java.util.Date
and PreparedStatement.setDate(int i, Date d) takes java.sql.Date .
but i am using arrays in which a counter is set , so will that make any difference because the error that i am recieving is
Thanks for the reply.
I do understand that SimpleDateFormat.parse(String source) returns java.util.Date
and PreparedStatement.setDate(int i, Date d) takes java.sql.Date .
but i am using arrays in which a counter is set , so will that make any difference because the error that i am recieving is
•
•
•
•
Error: 500
Internal Servlet Error:
org.apache.jasper.JasperException: Unable to compile class for JSPC:\j2sdkee1.2.1\repository\shiju-f70efc8fa\web\_0005cupdate_0002ejspupdate_jsp_11.java:191: Incompatible type for method. Can't convert java.lang.String to java.sql.Date.
ps.setDate(1,leavefrom[counter]);
^
C:\j2sdkee1.2.1\repository\shiju-f70efc8fa\web\_0005cupdate_0002ejspupdate_jsp_11.java:192: Incompatible type for method. Can't convert java.lang.String to java.sql.Date.
ps.setDate(2,until[counter]);
^
2 errors
at org.apache.jasper.compiler.Compiler.compile(Compiler.java:248)
at org.apache.jasper.runtime.JspLoader$2.run(JspLoader.java:273)
at java.security.AccessController.doPrivileged(Native Method)
at org.apache.jasper.runtime.JspLoader.loadJSP(JspLoader.java:270)
at org.apache.jasper.runtime.JspServlet$JspServletWrapper.loadIfNecessary(JspServlet.java:137)
at org.apache.jasper.runtime.JspServlet$JspServletWrapper.service(JspServlet.java:148)
at org.apache.jasper.runtime.JspServlet.serviceJspFile(JspServlet.java:247)
at org.apache.jasper.runtime.JspServlet.service(JspServlet.java:352)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:865)
at org.apache.tomcat.core.ServiceInvocationHandler.method(ServletWrapper.java:626)
at org.apache.tomcat.core.ServletWrapper.handleInvocation(ServletWrapper.java:534)
at org.apache.tomcat.core.ServletWrapper.handleRequest(ServletWrapper.java:378)
at org.apache.tomcat.core.Context.handleRequest(Context.java:644)
at org.apache.tomcat.core.ContextManager.service(ContextManager.java:440)
at org.apache.tomcat.service.http.HttpConnectionHandler.processConnection(HttpConnectionHandler.java:144)
at org.apache.tomcat.service.TcpConnectionThread.run(TcpEndpoint.java:310)
at java.lang.Thread.run(Thread.java:484)
•
•
Join Date: Jul 2008
Posts: 50
Reputation:
Solved Threads: 0
when i am using the code to convert string to sql.Date
I am facing the error given below because of the array so how should i code to convert it into sql.Date when the string[] is to be converted to sql.Date.Pls suggest me.
Thanks in advance.
Java Syntax (Toggle Plain Text)
DateFormat df = new SimpleDateFormat("dd/MM/yyyy"); Date d = df.parse(leavefrom); String dfrom=df.format(d); java.sql.Date dd = new java.sql.Date(d.getTime()); Date dt = df.parse(until); String dto=df.format(dt); java.sql.Date ddt = new java.sql.Date(dt.getTime());
I am facing the error given below because of the array so how should i code to convert it into sql.Date when the string[] is to be converted to sql.Date.Pls suggest me.
Thanks in advance.
•
•
•
•
Error: 500
Internal Servlet Error:
org.apache.jasper.JasperException: Unable to compile class for JSPC:\j2sdkee1.2.1\repository\shiju-f70efc8fa\web\_0005cupdate_0002ejspupdate_jsp_11.java:184: Incompatible type for method. Can't convert java.lang.String[] to java.lang.String.
Date d = df.parse(leavefrom);
^
C:\j2sdkee1.2.1\repository\shiju-f70efc8fa\web\_0005cupdate_0002ejspupdate_jsp_11.java:189: Incompatible type for method. Can't convert java.lang.String[] to java.lang.String.
Date dt = df.parse(until);
^
C:\j2sdkee1.2.1\repository\shiju-f70efc8fa\web\_0005cupdate_0002ejspupdate_jsp_11.java:201: [] can only be applied to arrays. It can't be applied to java.sql.Date.
ps.setDate(1,dd[counter]);
^
C:\j2sdkee1.2.1\repository\shiju-f70efc8fa\web\_0005cupdate_0002ejspupdate_jsp_11.java:202: [] can only be applied to arrays. It can't be applied to java.sql.Date.
ps.setDate(2,ddt[counter]);
^
4 errors
at org.apache.jasper.compiler.Compiler.compile(Compiler.java:248)
at org.apache.jasper.runtime.JspLoader$2.run(JspLoader.java:273)
at java.security.AccessController.doPrivileged(Native Method)
at org.apache.jasper.runtime.JspLoader.loadJSP(JspLoader.java:270)
at org.apache.jasper.runtime.JspServlet$JspServletWrapper.loadIfNecessary(JspServlet.java:137)
at org.apache.jasper.runtime.JspServlet$JspServletWrapper.service(JspServlet.java:148)
at org.apache.jasper.runtime.JspServlet.serviceJspFile(JspServlet.java:247)
at org.apache.jasper.runtime.JspServlet.service(JspServlet.java:352)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:865)
at org.apache.tomcat.core.ServiceInvocationHandler.method(ServletWrapper.java:626)
at org.apache.tomcat.core.ServletWrapper.handleInvocation(ServletWrapper.java:534)
at org.apache.tomcat.core.ServletWrapper.handleRequest(ServletWrapper.java:378)
at org.apache.tomcat.core.Context.handleRequest(Context.java:644)
at org.apache.tomcat.core.ContextManager.service(ContextManager.java:440)
at org.apache.tomcat.service.http.HttpConnectionHandler.processConnection(HttpConnectionHandler.java:144)
at org.apache.tomcat.service.TcpConnectionThread.run(TcpEndpoint.java:310)
at java.lang.Thread.run(Thread.java:484)
Last edited by peter_budo; May 5th, 2009 at 4:53 am. Reason: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks) and [icode] (inline code) tags.
•
•
Join Date: Nov 2007
Posts: 10
Reputation:
Solved Threads: 4
•
•
•
•
when i am using the code to convert string to sql.Date
Java Syntax (Toggle Plain Text)
DateFormat df = new SimpleDateFormat("dd/MM/yyyy"); Date d = df.parse(leavefrom); String dfrom=df.format(d); java.sql.Date dd = new java.sql.Date(d.getTime()); Date dt = df.parse(until); String dto=df.format(dt); java.sql.Date ddt = new java.sql.Date(dt.getTime());
I am facing the error given below because of the array so how should i code to convert it into sql.Date when the string[] is to be converted to sql.Date.Pls suggest me.
Thanks in advance.
Error: 500
Internal Servlet Error:
org.apache.jasper.JasperException: Unable to compile class for JSPC:\j2sdkee1.2.1\repository\shiju-f70efc8fa\web\_0005cupdate_0002ejspupdate_jsp_11.java:184: Incompatible type for method. Can't convert java.lang.String[] to java.lang.String.
Date d = df.parse(leavefrom);
^
C:\j2sdkee1.2.1\repository\shiju-f70efc8fa\web\_0005cupdate_0002ejspupdate_jsp_11.java:189: Incompatible type for method. Can't convert java.lang.String[] to java.lang.String.
Date dt = df.parse(until);
^
C:\j2sdkee1.2.1\repository\shiju-f70efc8fa\web\_0005cupdate_0002ejspupdate_jsp_11.java:201: [] can only be applied to arrays. It can't be applied to java.sql.Date.
ps.setDate(1,dd[counter]);
^
C:\j2sdkee1.2.1\repository\shiju-f70efc8fa\web\_0005cupdate_0002ejspupdate_jsp_11.java:202: [] can only be applied to arrays. It can't be applied to java.sql.Date.
ps.setDate(2,ddt[counter]);
^
4 errors
These errors help to understand the problem -
For 1st Error - use Date d = df.parse(leavefrom[counter]);
For 2nd error - use Date dt = df.parse(until[counter]);
and then in last 2 errors why are you using ps.setDate(1,dd[counter])
instead it should be ps.setDate(1,dd);
Last edited by deepalihanand; May 5th, 2009 at 2:23 pm.
![]() |
Similar Threads
- OpenGL Help (Game Development)
- I've got Trojan.Holax... is this bad? (Viruses, Spyware and other Nasties)
Other Threads in the JSP Forum
- Previous Thread: tabs in jsp
- Next Thread: Get last Array
| Thread Tools | Search this Thread |
apache array backbutton combobox comma connection csv database development directorystructure dropdownlist dynamicpagetitles eclipse frames glassfish ie8 imagetodatabse imageupload integer internet java javaee javascript jsf jsp jsppagetitles levels mvc2 mvcmodel2 mysql netbeans network parameters passing ping printinserverinsteadofclient read redirect request.getparameter response seperated servlet servletdopost()readxml sessions software sql ssl state_saving_method stocks sun tomcat tutorial update values video web write






