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

JSPJava 8Object Oriented ProgrammingProgramming

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
Published on 22-Mar-2019 10:44:26