How to print date and time in specific format using JSP?

SimpleDateFormat is a concrete class for formatting and parsing dates in a locale-sensitive manner. SimpleDateFormat allows you to start by choosing any user-defined patterns for date-time formatting.

Let us modify the above example as follows −


 Live Demo

<%@ page import = "*,java.util.*" %>
<%@ page import = "javax.servlet.*,java.text.*" %>
      <title>Display Current Date & Time</title>
         <h1>Display Current Date & Time</h1>
         Date dNow = new Date( );
         SimpleDateFormat ft =
         new SimpleDateFormat ("E yyyy.MM.dd 'at' hh:mm:ss a zzz");
         out.print( "<h2 align=\"center\">" + ft.format(dNow) + "</h2>");

Compile the above servlet once again and then call this servlet using the URL http://localhost:8080/CurrentDate. You will receive the following result −


Display Current Date & Time
Mon 2010.06.21 at 10:06:44 PM GMT+04:00

Samual Sam
Samual Sam

Learning faster. Every day.

Updated on: 30-Jul-2019


Kickstart Your Career

Get certified by completing the course

Get Started