Hi ,
I'm new to servlets and I'm trying out the Date function in servlets. Instead of printing todays date it is giving something like "Date@fbb7cb" this . Could you please point of my mistake .... Following is the code snippet

PrintWriter pw = response.getWriter();
			Date today = new Date();
			
			pw.println("<HTML>"+"<BODY>");
			pw.println(today+"</BODY>"+"</HTML>");

Note: ive made all the proper declarations

thanks

Edited 5 Years Ago by peter_budo: Correcting code tags

What you show for the print out looks like what an object returns to the toStrng() method if it is not overridden in the class. IE it uses the default Object toString()

What package is the Date class in?

The issue is that you aren't getting a string value from your Date object; as a result, it is printing the class and hash for today , rather than an useful information. At minimum, you need to use today.toString() , which would give the time and date in UTC format; better would be to use a SimpleDateFormat object to format the date in the manner you need it to appear in.

its in some default package called src ... im actually using the eclipse so there is one default package int src there

sorry ..... in source folder there is default package .... this file is there...

Look at the API doc for the Date class you are using and see if it defines a toString() method (I doubt it) or if there is another method to call that will generate what you want to see printed.

Edited 5 Years Ago by NormR1: n/a

@schoil: i tried .toString() but it didnt work .... i just wanna know the mistake instead of circumventing

thanks

The compiler will create the toString() method call for a class reference automatically. You don't have to call it specifically.

i just wanna know the mistake

What package is the Date class in? Do you have API doc for that class?

Edited 5 Years Ago by NormR1: n/a

Please note that i used the toString method below and i got "Date@162dbb6 " ...... does it have anything to do with browser compatability ... some versiion problem?

PrintWriter pw = response.getWriter();
			Date today = new Date();
			
			pw.println("<HTML>"+"<BODY>");
			pw.println(today.toString()+"</BODY>"+"</HTML>");

thanks

What package is the Date class in? Do you have API doc for that class?

Show the import statements for the class.

Edited 5 Years Ago by NormR1: n/a

Using a DateFormat is not a workaround; is the intended solution when formatting the standard Java Date object to a String . Mind you there are two very different Date classes in the standard Java library: java.util.Date , which is used to hold a generic date and time (as a millisecond counter from 1 Jan 1970 00:00.00, the same as a Unix time_t ), and java.sql.Date , which holds a SQL date value and cannot be used as a general date value. My guess is that you've imported the SQL Date by mistake (something which is easy to do in Eclipse, given how the auto-import tool works), as the utility Date class does in fact have a usable (if not very useful) toString() method. You should be able to tell by looking at the list of imports at the top of your source file.

Edited 5 Years Ago by Schol-R-LEA: n/a

Here's what I get with my compiler:

java.util.Date dt = new java.util.Date();
      System.out.println("dt=" + dt);    // dt=Mon Jun 20 09:24:37 CDT 2011

That looks like what I would expect from java.util.Date , NormR1. Good work testing that, however, as it demonstrates that the OP isn't using the standard Date class.

I did some checking, and the SQL Date class ought to print the date in YYYY-MM-DD format by default. I'm wondering just what Date class the OP is using. Again, if you could post the import list, it would be very helpful for us in figuring this out.

Edited 5 Years Ago by Schol-R-LEA: n/a

i tried the code you've posted, it worked fine for me... please check your config..
also, i've attached a project (based on the code you posted)
.

Hi ,
I'm new to servlets and I'm trying out the Date function in servlets. Instead of printing todays date it is giving something like "Date@fbb7cb" this . Could you please point of my mistake .... Following is the code snippet

PrintWriter pw = response.getWriter();
			Date today = new Date();
			
			pw.println("<HTML>"+"<BODY>");
			pw.println(today+"</BODY>"+"</HTML>");

Note: ive made all the proper declarations

thanks

Sorry for the trouble and mistake guys ....... its actually silly ........ ive named the class as Date .... so the error occured ....... now its working fine......
thanks

This question has already been answered. Start a new discussion instead.