How to print current date in JSP?

Use jsp:useBean to construct a java.util.Date instance and use JSTL fmt:formatDate to format it into a human readable string using a SimpleDateFormat pattern.

<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<jsp:useBean id="date" class="java.util.Date" />
Current year is: <fmt:formatDate value="${date}" pattern="yyyy" />

The old fashioned scriptlet way would be:

<%= new java.text.SimpleDateFormat("yyyy").format(new java.util.Date()) %>

Note that you need to specify the full qualified class name when you don’t use @page import directives, that was likely the cause of your problem. Using scriptlets is however highly discouraged since a decade.

This all is demonstrated in the [jsp] tag info page as well 🙂

Leave a Comment