[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 7003
  • Last Modified:

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
0
lilyyan
Asked:
lilyyan
  • 9
  • 6
  • 5
3 Solutions
 
koppchaCommented:
can you post the submissionFiles.jsp
0
 
koppchaCommented:
Just Guess:
   Could be some problem with the Date declaration in that file.
0
 
lilyyanAuthor Commented:
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.

0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
Jim CakalicSenior Developer/ArchitectCommented:
Hi,

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

Regards,
Jim Cakalic
0
 
koppchaCommented:
prefix all Date types with java.util
0
 
koppchaCommented:
java.util.Date d = new java.util.Date()
0
 
lilyyanAuthor Commented:
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.
0
 
koppchaCommented:
java.sql.* and java.util.* they both have Date so it is getting confused which one to you...now we made it specific
0
 
koppchaCommented:
>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.*
0
 
lilyyanAuthor Commented:
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 ?
0
 
Jim CakalicSenior Developer/ArchitectCommented:
There is both a java.util.Date and a java.sql.Date. If you import both java.util.* and java.sql.* then the compiler will not be able to resolve an unqualified reference to Date -- it will be ambiguous because there are at least two from which it might choose. Your solutions are 1) remove either java.util.* or java.sql.* from the jsp imports, 2) convert your wildcard imports to only import the specific classes needed from java.util and java.sql, 3) leave it as it is and always fully qualify the Date class so the compiler can resolve the reference.

Jim
0
 
koppchaCommented:
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 ?
0
 
lilyyanAuthor Commented:
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.



0
 
koppchaCommented:
>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  
0
 
Jim CakalicSenior Developer/ArchitectCommented:
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
0
 
lilyyanAuthor Commented:

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.
0
 
Jim CakalicSenior Developer/ArchitectCommented:
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.
0
 
Jim CakalicSenior Developer/ArchitectCommented:
Interestingly, I was reading the Java Language Spec a little more closely just now, and it seems that a single-type-import (e.g., java.util.Date) can "shadow" the declaration of a type-import-on-demand (e.g., java.sql.*) whereas a type-import-on-demand never "shadows" any other declaration.

Some examples:

This works and all references to Date understood to be java.util.Date:
    import java.sql.*;
    import java.util.Date;

Similarly, this works and all references to Date understood to be java.util.Date:
    import java.util.Date;
    import java.sql.*;

This does not work (as we well know) and causes a compile-time "ambiguous type" error:
    import java.util.*;
    import java.sql.*;

Does not work and causes a compile-time "collides with another type" error on java.sql.Date:
    import java.util.Date;
    import java.sql.Date;

You can read for yourself JLS sections 6.3.1 and 7.5:
    http://java.sun.com/docs/books/jls/

Jim
0
 
lilyyanAuthor Commented:
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 :)
0
 
koppchaCommented:
Thanks
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 9
  • 6
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now