JSP Date Handling with Examples
In this article, I am going to discuss JSP Date Handling i.e. How to Handle Dates in JSP Applications. Please read our previous article, where we discuss File Uploading and Downloading in JSP with Examples. In JSP, we can use all the methods available in core Java. The Date class is available in java.util package which encapsulates the current date and time. It supports two constructors:
- Date(): It initializes the object with the current date and time.
- Date(long millisec): It accepts one argument that equals the number of milliseconds that have elapsed since midnight.
- boolean after(Date date): It returns after the date of the current date parameter.
- boolean before(Date date): It returns the before the date of the given date parameter.
- Object clone() : It returns the duplicate of the invoking Date object.
- int compareTo(Date date): It compares the value of invoking object with the date and returns zero if the values are equal.
- int compareTo(Object obj): It compares compareTo(Date) of obj is of class Date otherwise throws ClassCastException.
- boolean equals(Object date): It returns true if both the date objects are equal otherwise returns false.
- long getTime(): It is used to fetch the time.
- int hashCode(): It is used to return a hash code of the invoking object.
- void setTime(longTime): It is used to set the time of the given date object.
- String toString(): It is used to convert the invoking Date object into a string and return the result.
Current Date and Time in JSP
In this example, we are using the date object to fetch the current date and time.
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <%@ page import="java.util.*"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Current Date</title> </head> <body> Today's date: <%=(new java.util.Date()).toLocaleString()%> </body> </html>
Date Comparison in JSP
Consider the following methods to compare two dates:
- Use getTime() method to get the number of milliseconds for both objects and then compare these two values.
- Use before(), after(), and equals() methods because the 12th of the month comes before the 18th and it returns true in this case.
- Use compareTo() methods which are defined by the Comparable interface and implemented by Date.
Date Formatting using SimpleDateFormat in JSP
SimpleDateFormat class is used for formatting and parsing dates in a locale-sensitive manner which allows you to start by choosing any user-defined patterns for date-time formatting.
<%@ page import = "java.io.*,java.util.*" %> <%@ page import = "javax.servlet.*,java.text.*" %> <html> <head> <title>Display Current Date & Time</title> </head> <body> <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>"); %> </body> </html>
DateFormat Format Codes in JSP
Use a time pattern string to specify the time format where all ASCII letters are reserved as pattern letters.
In the next article, I am going to discuss JSP Standard Tag Library (JSTL). Here, in this article, I try to explain Handling Date in JSP with Examples. I hope you enjoy this Handling Date in JSP with Examples article.
About the Author: Pranaya Rout
Pranaya Rout has published more than 3,000 articles in his 11-year career. Pranaya Rout has very good experience with Microsoft Technologies, Including C#, VB, ASP.NET MVC, ASP.NET Web API, EF, EF Core, ADO.NET, LINQ, SQL Server, MYSQL, Oracle, ASP.NET Core, Cloud Computing, Microservices, Design Patterns and still learning new technologies.