943,712 Members | Top Members by Rank

Ad:
  • JSP Discussion Thread
  • Marked Solved
  • Views: 4535
  • JSP RSS
Apr 29th, 2009
0

how to convert string values to date and compare them while using an array

Expand Post »
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

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();
here i am using 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.
Similar Threads
Reputation Points: 8
Solved Threads: 0
Junior Poster in Training
shijunair is offline Offline
50 posts
since Jul 2008
Apr 29th, 2009
1

Re: how to convert string values to date and compare them while using an array

First of all what type of argument does 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)
  1. String sDate = 01/04/2009; // 1st of April
  2. String format = "dd/MM/yyyy"
  3.  
  4. SimpleDateFormat sdf = new SimpleDateFormat(format);
  5. sdf.setLenient(false);
  6.  
  7. Date date = sdf.parse(sDate);

Check the API for more: SimpleDateFormat
Sponsor
Featured Poster
Reputation Points: 1014
Solved Threads: 446
Nearly a Senior Poster
javaAddict is offline Offline
3,258 posts
since Dec 2007
May 3rd, 2009
0

Re: how to convert string values to date and compare them while using an array

Click to Expand / Collapse  Quote originally posted by javaAddict ...
First of all what type of argument does 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)
  1. String sDate = 01/04/2009; // 1st of April
  2. String format = "dd/MM/yyyy"
  3.  
  4. SimpleDateFormat sdf = new SimpleDateFormat(format);
  5. sdf.setLenient(false);
  6.  
  7. 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)
  1. ps.setString(1,leavefrom[counter]);
  2. ps.setString(2,until[counter]);
leavefrom and until are in String type.
and i am comparing this with the database which is in
JSP Syntax (Toggle Plain Text)
  1. Date type
  2. i.e to_date, from_date
and because of this i am facing error while updating i.e some of them are updated and some are not. so i want to use 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)
  1. ps.setDate(1,leavefrom[counter]);
  2. ps.setDate(2,until[counter]);
to compare all the values but getting error because leavefrom and until are in string type.Pls have a look at the whole code which i have sent previously.
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.
Reputation Points: 8
Solved Threads: 0
Junior Poster in Training
shijunair is offline Offline
50 posts
since Jul 2008
May 4th, 2009
0

Re: how to convert string values to date and compare them while using an array

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.
Reputation Points: 10
Solved Threads: 4
Newbie Poster
deepalihanand is offline Offline
10 posts
since Nov 2007
May 4th, 2009
0

Re: how to convert string values to date and compare them while using an array

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.
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()
Sponsor
Featured Poster
Reputation Points: 1014
Solved Threads: 446
Nearly a Senior Poster
javaAddict is offline Offline
3,258 posts
since Dec 2007
May 4th, 2009
0

Re: how to convert string values to date and compare them while using an array

Click to Expand / Collapse  Quote originally posted by javaAddict ...
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()
According to me, MS-Access database require format before entering date field for more accuracy. Correct me, if I am wrong. This forum helps me in learning.
Thanks
Last edited by deepalihanand; May 4th, 2009 at 4:07 pm.
Reputation Points: 10
Solved Threads: 4
Newbie Poster
deepalihanand is offline Offline
10 posts
since Nov 2007
May 5th, 2009
0

Re: how to convert string values to date and compare them while using an array

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

Quote ...
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)
Reputation Points: 8
Solved Threads: 0
Junior Poster in Training
shijunair is offline Offline
50 posts
since Jul 2008
May 5th, 2009
0

Re: how to convert string values to date and compare them while using an array

when i am using the code to convert string to sql.Date
Java Syntax (Toggle Plain Text)
  1. DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
  2. Date d = df.parse(leavefrom);
  3. String dfrom=df.format(d);
  4.  
  5. java.sql.Date dd = new java.sql.Date(d.getTime());
  6.  
  7. Date dt = df.parse(until);
  8. String dto=df.format(dt);
  9.  
  10. 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.

Quote ...
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.
Reputation Points: 8
Solved Threads: 0
Junior Poster in Training
shijunair is offline Offline
50 posts
since Jul 2008
May 5th, 2009
0

Re: how to convert string values to date and compare them while using an array

Click to Expand / Collapse  Quote originally posted by shijunair ...
when i am using the code to convert string to sql.Date
Java Syntax (Toggle Plain Text)
  1. DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
  2. Date d = df.parse(leavefrom);
  3. String dfrom=df.format(d);
  4.  
  5. java.sql.Date dd = new java.sql.Date(d.getTime());
  6.  
  7. Date dt = df.parse(until);
  8. String dto=df.format(dt);
  9.  
  10. 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.
Reputation Points: 10
Solved Threads: 4
Newbie Poster
deepalihanand is offline Offline
10 posts
since Nov 2007
May 15th, 2009
0

Re: how to convert string values to date and compare them while using an array

Thanks for the reply deepalihanand,
I am very thankful to you that you have solved my query.I wont ever forget your help.
Thanks once again.
Bye take care.
Reputation Points: 8
Solved Threads: 0
Junior Poster in Training
shijunair is offline Offline
50 posts
since Jul 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in JSP Forum Timeline: tabs in jsp
Next Thread in JSP Forum Timeline: Get last Array





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC