<%@page import="java.util.*" %> <%@ page import="java.text.*" %> <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title>Displaying Dates</title> </head> <body> <h3>Displaying Today's date in different formats:</h3> <%! Date today=new Date(); DateFormat formatFull=DateFormat.getDateInstance(DateFormat.FULL); DateFormat formatLong=DateFormat.getDateInstance(DateFormat.LONG); DateFormat formatMedium=DateFormat.getDateInstance(DateFormat.MEDIUM); DateFormat formatShort=DateFormat.getDateInstance(DateFormat.SHORT); %> <p> Full Format: <%=formatFull.format(today) %> <br> Long Format: <%=formatLong.format(today) %> <br> Medium Format: <%=formatMedium.format(today) %> <br> Short Format: <%=formatShort.format(today) %> </body> </html>
Description:
- Use the import page directive to import the java.text.package.
- Create a Date object.
- Create a Date Format object by calling the getDateInstance method and passing DateFormat.FULL as a parameter.
- Create a Date Format object by calling the getDateInstance method and passing DateFormat.LONG as a parameter.
- Create a Date Format object by calling the getDateInstance method and passing DateFormat.MEDIUM as a parameter.
- Create a Date Format object by calling the getDateInstance method and passing DateFormat.SHORT as a parameter.
- Pass the current date as a parameter to the format method of the FormatFull object.This will display the full date, for example: 21 December 2012.
- Pass the current date as a parameter to the format method of the FormatLong object.This will display the long date, for example:12/21 /2012.
- Pass the current date as a parameter to the format method of the FormatMedium object.This will display the medium date, for example:Friday, 21 December 2012.
- Pass the current date as a parameter to the format method of the FormatShort object.This will display the short date, for example:12/21 /12.