Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Jsp Date Formatter in JSP

Related Tutorials


 
 

Share on Google+Share on Google+

Date Formatter in JSP

Advertisement
This section illustrates you how to use date formatter.

Date Formatter in JSP

     

This section illustrates you how to use date formatter.

To display the date in different formats, we have used DateFormat class. This class provides methods like getDateInstance(), getTimeInstance() and getDateTimeInstance(). The getDateInstance() method provides date format, getTimeInstance() method provides time format and getDateTimeInstance() method provides both date and time. It provides different styles like FULL, LONG, MEDIUM, SHORT. 

FULL- it specifies completely, such as Wednesday, July 9, 2008 AD or 6:13pm.
LONG- it specifies as July 9, 2008 or 6:13pm.
MEDIUM - it specifies as Jul 9,2008.
SHORT - it specifies as 09.07.08 or 6:13pm


Here is the code of date.jsp

<%@ page import="java.text.*,java.util.*" session="true"%>
<html>
<head><title>Date Format Example</title></head>
<%!
DateFormat date = DateFormat.getDateInstance
(DateFormat.FULL, Locale.US);
String result = date.format(new Date());
%>
<b>Today's date is: <%= result %></b>

In the above example, the method getDateInstance() is called. Pass the object date  to the format method on DateFormat object which shows the date.

We have given locale as US, date will be displayed as:

Now if we change the locale to FRANCE, the date will be displayed as:

Download Source Code

Advertisement

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: August 20, 2008

Related Tutorials

Discuss: Date Formatter in JSP  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:1
saifuctg
December 31, 2011
easy way

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