madonne
madonne

Reputation:

Displaying time in JSP page

This was my line in demo.jsp

<html>
<head>
<title>demo</title>
</head>
<body>

Hello there!!!   The time is <%= new java.util.Date()%>

</body>
</html>

which when i opened in firefox doesn't show the time. instead displays the same line:

"The time is <%= new java.util.Date()%>"

Upvotes: 5

Views: 47376

Answers (4)

Supercoder
Supercoder

Reputation: 1204

In order to be able to display the Date and/or the Time, the java libraries and out.println would be required displaying the time:

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page import="java.io.*,java.util.*, javax.servlet.*" %>
<%@page import="java.text.DateFormat"%>
<%@page import="java.text.SimpleDateFormat"%>

<html>
<body>
<p>&nbsp;</p>
<div align="center">
  <center>
  <table border="0" cellpadding="0" cellspacing="0" width="460" bgcolor="#EEFFCA">
    <tr>
      <td width="100%"><font size="6" color="#008000">&nbsp;Date Example</font></td>
    </tr>
    <tr>
      <td width="100%"><b>&nbsp;Current Date and time is:&nbsp; <font color="#FF0000">
      <% DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
   Date date = new Date(); out.println(dateFormat.format(date));%>
    </font></b></td>
    </tr>
  </table>
  </center>
</div>
</body>
</html>

Note: SimpleDateFormat allows you to display with different formats: e.g. "kk:mm:ss a z" or "yyyy/MM/dd HH:mm:ss.SSS", etc... kk=24 hour format, yyyy=year, MM=month, dd=day, HH=hour 24 hour format, hh=hour 1 to 12, mm=minute, ss=second, SSS=fraction of second, a =am or pm, and z =Time zone

Bottom line: To customize and display it, you would need to replace <%= new java.util.Date() %> with a more effective code:

Change from:

<%= new java.util.Date() %>

WITH the following format example:

<%@page import="java.io.*,java.util.*, javax.servlet.*" %>
<%@page import="java.text.DateFormat"%>
<%@page import="java.text.SimpleDateFormat"%>
    
<html>
.
.
<% DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
       Date date = new Date(); out.println(dateFormat.format(date));%>
.
.
</html>

Output:

enter image description here

Upvotes: 0

duffymo
duffymo

Reputation: 308743

If you go to this link, it has examples, with source, that show you how to do it properly. See the "Date" example under JSP 1.2 examples.

Another recommendation: Learn JSTL and use its format tags to format time and date properly. Don't use scriptlets.

Upvotes: 0

Rudolf Mayer
Rudolf Mayer

Reputation: 376

Your code is completely correct, this will display you the current time, formatted by the current locale settings. As others noted incorrectly, the position of where you put this in the page (i.e. surrounded by other HTML tags) is not the problem here.

However, it seems you are accessing your page either directly from the file location (file://yourPath/demo.jsp), or via a standard webserver (e.g. Apache), but not from a Servlet container (e.g. Tomcat, Jetty, ..), which would actually pre-process the <% -- %> JSP System tag.

Look for an example for how to configure Tomcat or Jetty for your operating system, and where to put the JSP pages, or for a tutorial on how to use it from within Eclipse or IDEA.

Upvotes: 5

Dave
Dave

Reputation: 7025

It looks like you're putting <%= new java.util.Date()%> in the wrong place, and it is being treated as text rather than code, it should look something like this:

<td width="100%"><b>&nbsp;Current  Date 
and time is:&nbsp; <font color="#FF0000">


<%= new java.util.Date() %>
</font></b></td>

If you post a code sample, it'll help a lot.

Some examples here too: http://www.roseindia.net/jsp/jsp_date_example.shtml

Dave

Upvotes: 4

Related Questions