Scriptlet error: The method getFullYear()/toLocaleDateString() is undefined for the type Date

158 views Asked by At

I am currently trying to use scriptlets to enter the current year or locale date into page title and description like below. On the console, I get the error that is mentioned in the titles, causing the "org.apache.jasper.JasperException: Unable to compile class for JSP" error.

<tiles:param name="pageTitle">something something <%=(new java.util.Date()).getFullYear()%> something something </tiles:param>
<tiles:param name="pageDescription">Last Update: <%=(new java.util.Date()).toLocaleDateString()%>. more something something</tiles:param>

What am I doing wrong, and how do I fix it? I have imported the java class Date as this at the beginning of the page:

<%@ page import="java.util.Date" %>

1

There are 1 answers

0
Sanjay On

You can try JSTL for printing date with some formatting.

  <%@ taglib prefix = "c" uri = "http://java.sun.com/jsp/jstl/core" %>
  <%@ taglib prefix = "fmt" uri = "http://java.sun.com/jsp/jstl/fmt" %>
  
      <c:set var = "now" value = "<% = new java.util.Date()%>" />
      <p>Formatted Date (1): <fmt:formatDate type = "time" value = "${now}" /></p>
      <p>Formatted Date (2): <fmt:formatDate type = "date" value = "${now}" /></p>
      <p>Formatted Date (3): <fmt:formatDate type = "both" value = "${now}" /></p>
      <p>Formatted Date (4): <fmt:formatDate type = "both" dateStyle = "short" timeStyle = "short" value = "${now}" /></p>
      <p>Formatted Date (5): <fmt:formatDate type = "both" dateStyle = "medium" timeStyle = "medium" value = "${now}" /></p>
      <p>Formatted Date (6): <fmt:formatDate type = "both" dateStyle = "long" timeStyle = "long" value = "${now}" /></p>
      <p>Formatted Date (7): <fmt:formatDate pattern = "yyyy-MM-dd" value = "${now}" /></p>