Thursday, August 29, 2013

JSP: Get Current Date

Short snippet showing how to get current date in a JSP using scriplet code. In occasions it's needed to have the date served by the server instead of using JavaScript code, which depends of right configuration in user's machine, or differences may arise due to distinct time zones.

<%@ page import="java.util.*" %>
<%@ page import="java.text.SimpleDateFormat"%>
 
<%
   Date dNow = new Date();
   SimpleDateFormat ft = 
   new SimpleDateFormat ("MM/dd/yyyy");
   String currentDate = ft.format(dNow);
%>

<p>The current date is: <%=currentDate%></p>

No comments:

Post a Comment