JSP Date Handling

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:

  1. Date(): It initializes the object with the current date and time.
  2. Date(long millisec): It accepts one argument that equals the number of milliseconds that have elapsed since midnight.
Methods
  1. boolean after(Date date): It returns after the date of the current date parameter.
  2. boolean before(Date date): It returns the before the date of the given date parameter.
  3. Object clone() : It returns the duplicate of the invoking Date object.
  4. int compareTo(Date date): It compares the value of invoking object with the date and returns zero if the values are equal.
  5. int compareTo(Object obj): It compares compareTo(Date) of obj is of class Date otherwise throws ClassCastException.
  6. boolean equals(Object date): It returns true if both the date objects are equal otherwise returns false.
  7. long getTime(): It is used to fetch the time.
  8. int hashCode(): It is used to return a hash code of the invoking object.
  9. void setTime(longTime): It is used to set the time of the given date object.
  10. 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.

CurrentDate.jsp
<%@ 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>
Output

How to Handle Dates in JSP Applications

Date Comparison in JSP

Consider the following methods to compare two dates:

  1. Use getTime() method to get the number of milliseconds for both objects and then compare these two values.
  2. Use before(), after(), and equals() methods because the 12th of the month comes before the 18th and it returns true in this case.
  3. 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.

Example: CurrentDate.jsp
<%@ 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>
Output

JSP Date Handling

DateFormat Format Codes in JSP

Use a time pattern string to specify the time format where all ASCII letters are reserved as pattern letters.

Handling Date in JSP with Examples

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.

Leave a Reply

Your email address will not be published. Required fields are marked *