sliziky
sliziky

Reputation: 139

Displaying date in simple JSP program

I've copied the following JSP code from another page but when I view it from a browser, the date does not display.

<HTML>
<BODY>
Hello!  The time is now <%= new java.util.Date() %>
</BODY>
</HTML>

I've saved it as file.jsp.

Upvotes: 1

Views: 15148

Answers (2)

Rajesh N
Rajesh N

Reputation: 2574

Try this (file.jsp):

<%@ page import = "java.util.Date" %>
<%@ page import = "java.text.SimpleDateFormat" %>
<html>
<head>
<%
  SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  String date = sdf.format(new Date());
%>
</head>
<body>
<p>Hello!  The time is now <%=date%></p>
</body>
</html>

You can change the date format that suits you in "yyyy-MM-dd HH:mm:ss". And, <%=date%> displays the result. Good luck mate :)

Result:

Hello! The time is now 2015-04-07 11:25:47

Note: You have to save this file and run it from a webserver like Tomcat.

Upvotes: 3

headlikearock
headlikearock

Reputation: 695

You need to add the following as the first line:

 <%@page contentType="text/html" import="java.util.*" %>

A tutorial can be found here: http://www.java-samples.com/showtutorial.php?tutorialid=81

Upvotes: 0

Related Questions