JSP Program for display Date

By: aathishankaran Emailed: 1589 times Printed: 2037 times    

Latest comments
By: rohit kumar - how this program is work
By: Kirti - Hi..thx for the hadoop in
By: Spijker - I have altered the code a
By: ali mohammed - why we use the java in ne
By: ali mohammed - why we use the java in ne
By: mizhelle - when I exported the data
By: raul - no output as well, i'm ge
By: Rajesh - thanx very much...
By: Suindu De - Suppose we are executing

Here the program to display the date is given by executing this program we can get current date and time. Here the program shows you how to create a simple dynamic JSP page that prints the current date and time. 

So the following code accomplish this:

 <%@page contentType="text/html" import="java.util.*" %>
<!--  http://www.java-samples.com/jsp  -->  
<html>
<body>
<p>&nbsp;</p>
<div align="center">
<center>
<table border="0" cellpadding="0" cellspacing="0" width="460" bgcolor="#EEFFCA">
<tr>
<td width="100%"><font size="6" color="#008000">&nbsp;Date Example</font></td>
</tr>
<tr>
<td width="100%"><b>&nbsp;Current Date and time is:&nbsp; <font color="#FF0000">
<%= new java.util.Date() %>
</font></b></td>
</tr>
</table>
</center>
</div>
</body>
</html>  

The main concept in this program is Date() function of the java.util package which returns the current data and time. 
 In the JSP Declaratives

<%@page contentType="text/html" import="java.util.*" %>

we are importing the java.util package and following JSP Expression code 

<%= new java.util.Date() %>


JSP Home | All JSP Tutorials | Latest JSP Tutorials

Sponsored Links

If this tutorial doesn't answer your question, or you have a specific question, just ask an expert here. Post your question to get a direct answer.



Bookmark and Share

Comments(24)


1. View Comment

It's very useful..I like u

View Tutorial          By: jeeva at 2008-04-03 23:58:41
2. View Comment

This code was quite helpful .I wud like to ask code for making text fields for username and password. Is it same as we make in HTML?????????

View Tutorial          By: Namrata at 2008-08-03 04:09:06
3. View Comment

Yes it is exactly same as HTML. No change. In fact you can convert any existing HTML page to a JSP page just be renaming to .jsp

View Tutorial          By: aathishankaran at 2008-08-03 05:22:38
4. View Comment

Thanks for good atricle. But this code will always show the client page time NOT server current time. If you want to use show server current time on client page then you have to use Ajax. One good post on the same topic here

http://binodsuman.blogspot.com/2009/05/get-current-server-time-on-client-page.html

Thanks,

Binod Suman
http://binodsuman.blogspot.com


View Tutorial          By: Binod Suman at 2009-05-28 09:40:48
5. View Comment

Hi Binod,

Since JSP code is executed at the server side before sending back to the browsers, the above code will also show the server time. (NOT CLIENT TIME).

However, your AJAX code also works.


View Tutorial          By: Aathi at 2009-05-28 21:20:10
6. View Comment

Thanks for the code it save's me a lot of work, however how can i delete the timezone.

View Tutorial          By: Wishkah at 2009-09-04 00:29:16
7. View Comment

Getting coding from this is very easy to me...

View Tutorial          By: shanmugaraja at 2009-09-09 01:50:02
8. View Comment

I have my application running on server that is there in some other time zone. I need to display the date and time zone of the user system on the home page of the application. Curently it is displaying the date and time of the zone where the server is located. I have used alomost each and every JavaAPI related to this in my JSP but again and again it is showing me the server's date and time.

View Tutorial          By: vitamin b12 at 2009-12-04 10:51:56
9. View Comment

Use (JavaScript) below code to display client date and time on web page


<HTML><HEAD><TITLE></TITLE>
<META http-equiv=Content-Type content=\"text/html; charset=iso-8859-1\">
<STYLE type=text/css>.mainheading {     FONT-WEIGHT: bold; FONT-SIZE: 14px; COLOR: #000000; FONT-FAMILY: Verdana, Arial, Helvetica, sans-serif }.mainbody {     FONT-SIZE: x-small; FONT-FAMILY: Verdana, Arial, Helvetica, sans-serif; LIST-STYLE-TYPE: square}.maintitle { FONT-WEIGHT: bold; FONT-SIZE: medium; FONT-FAMILY: Verdana, Arial, Helvetica, sans-serif}</STYLE>
</HEAD>
<BODY text=#000000 bgColor=#ffffff leftMargin=1 topMargin=0 onload=\"setFromTo();\">
<TABLE cellSpacing=3 cellPadding=0 width=770 border=0>
 <TBODY>
 <TR>
   <TD width=77>&nbsp;</TD>
   <TD class=mainheading>
     <P>&nbsp;</P>

   </TD>
 </TR>
 <TR>
   <TD colSpan=2>
     <HR color=green>
   </TD>
 </TR>
 <TR>
   <TD width=77>&nbsp;</TD>
   <TD><P></P>
<P>Today\'s Date & Time on Client machine </P>
<SCRIPT LANGUAGE=\"JavaScript\">
<!--
tday  =new Array(\"Sun\",\"Mon\",\"Tue\", \"Wed\",\"Thu\",\"Fri\",\"Sat\ ");
tmonth=new Array(\"January\",\"February\" ,\"March\",\"April\",\"May\",\ "June\",\"July\",\"August\",\" September\",\"October\",\" November\",\"December\");
function GetClock(){
d = new Date();
nday   = d.getDay();
nmonth = d.getMonth();
ndate  = d.getDate();
nyeara = d.getYear();
nhour  = d.getHours();
nmin   = d.getMinutes();
nsec   = d.getSeconds();
if(nyeara<1000){nyeara=(\"\"+( nyeara+11900)).substring(1,5); }
else{nyeara=(\"\"+(nyeara+ 10000)).substring(1,5);}
    if(nhour ==  0) {ap = \" AM\";nhour = 12;}
else if(nhour <= 11) {ap = \" AM\";}
else if(nhour == 12) {ap = \" PM\";}
else if(nhour >= 13) {ap = \" PM\";nhour -= 12;}
if(nmin <= 9) {nmin = \"0\" +nmin;}
if(nsec <= 9) {nsec = \"0\" +nsec;}
document.getElementById(\' clockbox\').innerHTML=\"\"+ tday[nday]+\", \"+tmonth[nmonth]+\" \"+ndate+\", \"+nyeara+\" \"+nhour+\":\"+nmin+\":\"+ nsec+ap+\"\";
setTimeout(\"GetClock()\", 1000);
}
window.onload=function(){ GetClock();}
//--></script>
<div id=\"clockbox\"></div>
<P></P>
<P> Admin</P>
  <P class=mainbody>&nbsp;</P>
   </TD>
 </TR>
 <TR>
   <TD width=77>&nbsp;</TD>
   <TD class=mainbody></TD>

 </TR>
 <TR>
   <TD colSpan=2>
     <HR color=green>
   </TD>
 </TR>
 </TBODY>
</TABLE>
</BODY>
</HTML>


View Tutorial          By: pmprash at 2009-12-22 01:15:33
10. View Comment

hey...i have a problem getting the system date in jsp and putting that date back into the oracle database...can you help over this.
i can get the system date but i am not able to take in the date value in a variable and then inserting it in the backend.


View Tutorial          By: angel at 2010-04-03 09:27:38
11. View Comment

hi.. how can i extrate day, month, year from date function

View Tutorial          By: avkash at 2010-10-03 05:19:58
12. View Comment

good code

View Tutorial          By: neha at 2011-07-02 01:46:52
13. View Comment

can we print only second by using the date function?

View Tutorial          By: mukesh kumar at 2011-07-27 02:31:02
14. View Comment

What is this....for simple question every one was commenting.....and unnecessarily praising...gud code gud code!!
Now why dont you people answer for question here in post no 10 and 11!!
Even I have a similar question.......as to how to put sql query in jsp that contains date condition.......that is i want to retrieve data between a particular date.....how to put date in jsp page for the query; i am using net beans


View Tutorial          By: Kaify at 2011-07-27 21:35:52
15. View Comment

Hei Kaify What is this????
if we get knowledge from any resource,it is always very good....
please keep u r manners....
don't irritate anyone man...


View Tutorial          By: Sooraj A.S at 2011-08-26 14:12:36
16. View Comment

<%!
DateFormat date = DateFormat.getDateInstance
(DateFormat.FULL, Locale.US);
String result = date.format(new Date());
%>
<%=result%>


View Tutorial          By: saifuctg at 2011-12-30 22:24:54
17. View Comment

only last code is gud one (DateFormat )... otherwise all is rubbish...

View Tutorial          By: Gaurav at 2013-01-04 09:18:43
18. View Comment

Hey i see some lines of code in order to displpay the system time where server is in another timezone. Seriously i need to put up that many line of code? Can any one help me with a simple code.
Good post Mod
Thanks


View Tutorial          By: Bala at 2013-01-09 13:58:48
19. View Comment

nice code....
nice explanation.. thanks


View Tutorial          By: vinitha at 2013-01-10 10:59:02
20. View Comment

not bad

View Tutorial          By: JUST_PREET at 2013-03-06 11:37:15
21. View Comment

It's good working very well...............

View Tutorial          By: avinash at 2013-03-07 09:38:21
22. View Comment

Can Some body help me??
i want To show date like "29. Apr 2013"
i m using Following Code ..
<fmt:formatDate pattern="dd. MMM yyyy" value="${now}" />

but it Gives me "29. 4 2013 "
instead of "29. Apr 2013"
waiting for ur suggestion...


View Tutorial          By: saif at 2013-04-29 09:22:31
23. View Comment

It is very helpful, I searched in many websites, but this post helped me the most to show date in jsp...

Thank you very much.......


View Tutorial          By: Anil Vaja at 2013-10-14 17:21:42
24. View Comment

This code works for me. But i have a doubt how to develop a Ajax program that gets the user input from a textbox and displays it along with the server's date and time.

View Tutorial          By: Daya at 2015-05-04 12:04:19

Your name (required):


Your email(required, will not be shown to the public):


Your sites URL (optional):


Your comments:



More Tutorials by aathishankaran
Web Security Issues
The Web User's Perspective
Server-side plug-Ins
The best way to avoid security vulnerabilities with new server
JavaScript Security
Window Object
Working with Status Bar Messages
Retrieving a Portion of a String
Referencing Windows
Math Object
Frame Object
Document Object
Closing Windows
Built-in Object in Javascript
Textarea Object

More Tutorials in JSP
LifecycleException: service.getName(): "Catalina"; Protocol handler start failed: `java.net.BindException: Permission denied <null>:80
JSP Alert Example
JSP CheckBox Example
Uploading an Image to a Database using JSP
Uploading a file to a server using JSP
A JSP page that gets properties from a bean
The page Directive in JSP
The taglib, tag, include, attribute and the variable Directive in JSP
Declarations in JSP
Scriptlets and Expressions in JSP
Tag Libraries in JSP
The Request Object in JSP
The Response Object in JSP
The Out Object in JSP
The Session Object in JSP

More Latest News
Most Viewed Articles (in JSP )
JSP Example to connect to MS SQL database and retrieve records
JSP Program for display Date
JSP CheckBox Example
What are the different scopes in JSP?
Comparison operators in JSP
JSP Alert Example
The Advantages of JSP
Sending Email using JSP
Uploading an Image to a Database using JSP
Arithmetic Evaluation Using the Expression Language in JSP
Tags using in jsp
Writing your first JSP page
Automatically Refreshing a JSP
JSP Example to connect to MS SQL database using Tomcat Connection Pool
Calling JSP from a batch file
Most Emailed Articles (in JSP)
Click to Activate and Use this control
NotifyUtil::java.net.ConnectException: Connection refused: connect
Document Object
Comparison operators in JSP
The Servlet Life Cycle
Cookies using JSP or Java Bean
Embedding java codes in jsp sciptlets
JSP pages in servlet
JSP Program for display Date
HttpClient
The Advantages of Servlets Over “Traditional” CGI
Closing Windows
Web Security Issues
Disabling Scriptlets in JSP using web.xml
Enable/Disable Scripting Elements in JSP