Link to home
Start Free TrialLog in
Avatar of lilyyan
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.DefaultErrorHandler.javacError(DefaultErrorHandler.java:84)
      org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:328)
      org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:397)
      org.apache.jasper.compiler.Compiler.compile(Compiler.java:288)
      org.apache.jasper.compiler.Compiler.compile(Compiler.java:267)
      org.apache.jasper.compiler.Compiler.compile(Compiler.java:255)
      org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:556)
      org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:296)
      org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:295)
      org.apache.jasper.servlet.JspServlet.service(JspServlet.java:245)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
-------------------

even if , i comment out all other lines, i still got the error.

Thank you for your reply
Avatar of koppcha
koppcha
Flag of United States of America image

can you post the submissionFiles.jsp
Just Guess:
   Could be some problem with the Date declaration in that file.
Avatar of lilyyan
lilyyan

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.

Avatar of Jim Cakalic
Hi,

Did you remember to import either java.util.Date or java.sql.Date in submissionFiles.jsp?

Regards,
Jim Cakalic
ASKER CERTIFIED SOLUTION
Avatar of koppcha
koppcha
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
java.util.Date d = new java.util.Date()
Avatar of lilyyan

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.
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.*
Avatar of lilyyan

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 ?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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 ?
Avatar of lilyyan

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.



>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  
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
Avatar of lilyyan

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.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of lilyyan

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 :)
Thanks