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"%>
<head><title>Date Format Example</title></head>
DateFormat date = DateFormat.getDateInstance
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:
We have 1000s of tutorials on our website. Search Tutorials tutorials on our website.
Posted on: August 20, 2008 If you enjoyed this post then why not add us on Google+? Add us to your Circles