lilyyan
asked on
get current time in jsp
Hi, jsp gurus,
I'm trying to insert a date into a mysql table. The format will be HH:mm yyyy-MM-dd
when i use the following code in a jsp file (file name curTime.jsp)
<%
Date currentDate = new Date(); //gets the current date
java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat ("HH:mm yyyy-MM-dd");
dateAsString = sdf.format( currentDate );
%>
the above code works fine in curTime.jsp file . My application is, in a html file, there is a form( used for upload a file), it will call another jsp file( submissionFiles.jsp). when i put the above code in submissionFiles.jsp, it always give me the error
-------------------------
error message :
Generated servlet error:
The type Date is ambiguous
org.apache.jasper.compiler .DefaultEr rorHandler .javacErro r(DefaultE rrorHandle r.java:84)
org.apache.jasper.compiler .ErrorDisp atcher.jav acError(Er rorDispatc her.java:3 28)
org.apache.jasper.compiler .JDTCompil er.generat eClass(JDT Compiler.j ava:397)
org.apache.jasper.compiler .Compiler. compile(Co mpiler.jav a:288)
org.apache.jasper.compiler .Compiler. compile(Co mpiler.jav a:267)
org.apache.jasper.compiler .Compiler. compile(Co mpiler.jav a:255)
org.apache.jasper.JspCompi lationCont ext.compil e(JspCompi lationCont ext.java:5 56)
org.apache.jasper.servlet. JspServlet Wrapper.se rvice(JspS ervletWrap per.java:2 96)
org.apache.jasper.servlet. JspServlet .serviceJs pFile(JspS ervlet.jav a:295)
org.apache.jasper.servlet. JspServlet .service(J spServlet. java:245)
javax.servlet.http.HttpSer vlet.servi ce(HttpSer vlet.java: 802)
-------------------
even if , i comment out all other lines, i still got the error.
Thank you for your reply
I'm trying to insert a date into a mysql table. The format will be HH:mm yyyy-MM-dd
when i use the following code in a jsp file (file name curTime.jsp)
<%
Date currentDate = new Date(); //gets the current date
java.text.SimpleDateFormat
dateAsString = sdf.format( currentDate );
%>
the above code works fine in curTime.jsp file . My application is, in a html file, there is a form( used for upload a file), it will call another jsp file( submissionFiles.jsp). when i put the above code in submissionFiles.jsp, it always give me the error
-------------------------
error message :
Generated servlet error:
The type Date is ambiguous
org.apache.jasper.compiler
org.apache.jasper.compiler
org.apache.jasper.compiler
org.apache.jasper.compiler
org.apache.jasper.compiler
org.apache.jasper.compiler
org.apache.jasper.JspCompi
org.apache.jasper.servlet.
org.apache.jasper.servlet.
org.apache.jasper.servlet.
javax.servlet.http.HttpSer
-------------------
even if , i comment out all other lines, i still got the error.
Thank you for your reply
can you post the submissionFiles.jsp
Just Guess:
Could be some problem with the Date declaration in that file.
Could be some problem with the Date declaration in that file.
ASKER
hi, koppcha , thnaks for your reply
well , i don't think the error is other lines of code. because if i commend out :
<%--
<%
Date currentDate = new Date(); //gets the current date
java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat ("HH:mm yyyy-MM-dd");
dateAsString = sdf.format( currentDate );
%>
--%>
everything works fine. if i comment out all other lines, i still got the above error.
well , i don't think the error is other lines of code. because if i commend out :
<%--
<%
Date currentDate = new Date(); //gets the current date
java.text.SimpleDateFormat
dateAsString = sdf.format( currentDate );
%>
--%>
everything works fine. if i comment out all other lines, i still got the above error.
Hi,
Did you remember to import either java.util.Date or java.sql.Date in submissionFiles.jsp?
Regards,
Jim Cakalic
Did you remember to import either java.util.Date or java.sql.Date in submissionFiles.jsp?
Regards,
Jim Cakalic
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
java.util.Date d = new java.util.Date()
ASKER
Hi, thanks for all promt replies.
jim_cakalic
yeah, i imported the java.util.* and java.sql.* in the submissionFiles.jsp.
i assign the variable dateAsString as a string
koppcha
no the above code is changed
java.util.Date currentDate = new java.util.Date();
it works now. thanks a lot for your suggestion.
just a little courious that i already import the java.util.*. i still need to specify again
if the above code in the curTime.jsp file. it works also.
thanks all attention.
jim_cakalic
yeah, i imported the java.util.* and java.sql.* in the submissionFiles.jsp.
i assign the variable dateAsString as a string
koppcha
no the above code is changed
java.util.Date currentDate = new java.util.Date();
it works now. thanks a lot for your suggestion.
just a little courious that i already import the java.util.*. i still need to specify again
if the above code in the curTime.jsp file. it works also.
thanks all attention.
java.sql.* and java.util.* they both have Date so it is getting confused which one to you...now we made it specific
>just a little courious that i already import the java.util.*. i still need to specify again
Not necessary if you haven't imported java.sql.*
Not necessary if you haven't imported java.sql.*
ASKER
Hi, koppcha, thanks for your reply
my above post is not clear.
update
1. in the submissionFiles.jsp, i already imported java.util.* and java.sql.*
but it turns out that i need to specify the java.util.Date again
in java.util.Date currentDate = new java.util.Date(); // add java.util as you suggested
maybe it's because i use mysql in the submissionFiles.jsp ?
2. a field called currentTime field is a string (varchar) in the table, so i don't use java.sql.Date package. the data is inserted as a string.
so suppose there is no confliction between java.util.Date and java.sql.Date ?
my above post is not clear.
update
1. in the submissionFiles.jsp, i already imported java.util.* and java.sql.*
but it turns out that i need to specify the java.util.Date again
in java.util.Date currentDate = new java.util.Date(); // add java.util as you suggested
maybe it's because i use mysql in the submissionFiles.jsp ?
2. a field called currentTime field is a string (varchar) in the table, so i don't use java.sql.Date package. the data is inserted as a string.
so suppose there is no confliction between java.util.Date and java.sql.Date ?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Sorry i didnt get this please clarify
>a field called currentTime field is a string (varchar) in the table, so i don't use java.sql.Date package. >the data is inserted as a string.
>so suppose there is no confliction between java.util.Date and java.sql.Date ?
>a field called currentTime field is a string (varchar) in the table, so i don't use java.sql.Date package. >the data is inserted as a string.
>so suppose there is no confliction between java.util.Date and java.sql.Date ?
ASKER
Hi, thanks for all replies.
to koppcha,
since currentTime field is a string (data type is varchar, not date ) in the table, so in jsp file, the sql statement will use setString () to insert the current time., instead of setDate().
since i'm not using java.sql.Date class here, this will not be the reason for the above error.
to jim_cakalic
java.sql.Date is a sub class of java.util.Date. Date() is a constructor of java.util.Date. (super class). suppose the complier won't get confused.
to koppcha,
since currentTime field is a string (data type is varchar, not date ) in the table, so in jsp file, the sql statement will use setString () to insert the current time., instead of setDate().
since i'm not using java.sql.Date class here, this will not be the reason for the above error.
to jim_cakalic
java.sql.Date is a sub class of java.util.Date. Date() is a constructor of java.util.Date. (super class). suppose the complier won't get confused.
>since i'm not using java.sql.Date class here, this will not be the reason for the above error.
In your code the compiler is getting confused at this statement
Date currentDate = new Date();// by doing this which Date you are referring to?compiler doen't know
In your code the compiler is getting confused at this statement
Date currentDate = new Date();// by doing this which Date you are referring to?compiler doen't know
Hi, lilyyan.
You are correct that java.sql.Date is a subclass of java.util.Date. But just because the javadoc doesn't say that java.sql.Date has a no-arg constructor does not mean that such does not exist. In fact, because Java does not support inheritance of constructors, java.sql.Date _must_ have a no-arg constructor if you are able to instantiate it that way. (Look at the source in your JDK's src.zip :-)
Besides that, the compiler just isn't as smart as you think it is. java.sql.Date, for example, doesn't implement a constructor that takes a String argument whereas java.util.Date does -- although it is deprecated. Nevertheless, if I write this small test program:
import java.util.*;
import java.sql.*;
public class AmbiguousDate {
public static void main(String[] args) {
Date now = new Date("Sat, 12 Aug 1995 13:30:00 GMT");
}
}
It will _not_ compile. Even though only one class in the hierarchy has a constructor that takes a String, java.util.Date, the compiler doesn't consider that. It looks at the namespaces you've declared using your imports and finds that more than one could satisfy the class reference for Date. In defeat it declares "Date type is ambiguous." No class file.
This is exactly what is happening to you. As I mentioned in my previous post, you have several options. Based on subsequent discussion, if you are really not using any classes from java.sql in your JSP then I would recommend removing that import.
Jim
You are correct that java.sql.Date is a subclass of java.util.Date. But just because the javadoc doesn't say that java.sql.Date has a no-arg constructor does not mean that such does not exist. In fact, because Java does not support inheritance of constructors, java.sql.Date _must_ have a no-arg constructor if you are able to instantiate it that way. (Look at the source in your JDK's src.zip :-)
Besides that, the compiler just isn't as smart as you think it is. java.sql.Date, for example, doesn't implement a constructor that takes a String argument whereas java.util.Date does -- although it is deprecated. Nevertheless, if I write this small test program:
import java.util.*;
import java.sql.*;
public class AmbiguousDate {
public static void main(String[] args) {
Date now = new Date("Sat, 12 Aug 1995 13:30:00 GMT");
}
}
It will _not_ compile. Even though only one class in the hierarchy has a constructor that takes a String, java.util.Date, the compiler doesn't consider that. It looks at the namespaces you've declared using your imports and finds that more than one could satisfy the class reference for Date. In defeat it declares "Date type is ambiguous." No class file.
This is exactly what is happening to you. As I mentioned in my previous post, you have several options. Based on subsequent discussion, if you are really not using any classes from java.sql in your JSP then I would recommend removing that import.
Jim
ASKER
yeah, i guess this is the reason. but the question is: Date() is a constructor of java.util.Date. and java.sql.Date is a sub class of java.util.Date. suppose the complier won't get confused.
also even if i use :
java.util.Date d = new Date() ; // no java.util. in second Date
still get the above error.
anyway, i'm glad that the problem is solved.
Oops. Mea culpa. I stand corrected.
java.sql.Date doesn't have a no-arg constructor. If you tried this:
java.sql.Date now = new java.sql.Date();
it wouldn't compile either.
java.sql.Date doesn't have a no-arg constructor. If you tried this:
java.sql.Date now = new java.sql.Date();
it wouldn't compile either.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hi, thanks all. very clear.
acception will be given to koppcha for the suggestion, and assist answers will be given to jim_cakalic. hope i'm fair
I do learn someting interesting :)
acception will be given to koppcha for the suggestion, and assist answers will be given to jim_cakalic. hope i'm fair
I do learn someting interesting :)
Thanks