Methods Associated with Date Class
To play with dates, you can use any of the following support methods once you have a Date object:
- boolean after(Date date): If the invoking Date object has a date that is later than the one indicated by date, it returns true; otherwise, it returns false.
- boolean before(Date date): If the invoking Date object has a date that is earlier than the one indicated by date, it returns true; otherwise, it returns false.
- Object clone( ): Duplicates the invoking Date object.
- int compareTo(Date date): The value of the invoking object is compared to the value of date. If the values are equal, returns 0. If the invoking object is older than date, it returns a negative value. If the invoking object is older than date, it returns a positive value.
- int compareTo(Object obj): If obj is of the class Date, it behaves exactly like compareTo(Date). Otherwise, a ClassCastException is thrown.
- boolean equals(Object date): If the invoking Date object has the same time and date as the one supplied by date, it returns true; otherwise, it returns false.
- long getTime( ): Since January 1, 1970, this function returns the number of milliseconds that have passed.
- int hashCode( ): The invoking object's hash code is returned.
- void setTime(long time): Sets the time and date to the value supplied by time, which is a millisecond value from midnight on January 1, 1970.
- String toString( ): Returns the result of converting the calling Date object to a string.
How to get the current date and time in JSP?
It is fairly simple to obtain the current date and time using the JSP software. To print the current date and time, use a simple Date object and the toString() method like follows:
<%@ page import = "java.io.*,java.util.*, javax.servlet.*" %>
<html>
<head>
<title>Display Current Date & Time</title>
</head>
<body>
<center>
<h1>Display Current Date & Time</h1>
</center>
<%
Date date = new Date();
out.print( "<h2 align = \"center\">" +date.toString()+"</h2>");
%>
</body>
</html>
Let's retain the code in CurrentDate.jsp and use the URL http://localhost:8080/CurrentDate.jsp to call this JSP. As a result, you'll get the following:

Use the URL http://localhost:8080/CurrentDate.jsp to refresh the page. Every time you refresh, you will see a difference in seconds.
Date Comparison
In your JSP scripts, you can use all of the accessible Java methods, as covered in the preceding sections. Consider the following ways if you need to compare two dates:
- You may obtain the number of milliseconds that have elapsed since midnight, January 1, 1970, for both objects using the getTime() method, and then compare these two results.
- Because the 12th of the month arrives before the 18th, you can use the methods before(), after(), and equals(); for example, new Date(99, 2, 12).before(new Date (99, 2, 18)) returns true.
- It is possible to use the compareTo() method, which is defined by the Comparable interface and implemented by Date.
Date Formatting using SimpleDateFormat
SimpleDateFormat is a concrete class for locale-sensitive date formatting and parsing. SimpleDateFormat allows you to begin by selecting any user-defined date-time formatting patterns.
Let's change the example above to something like this:
<%@ page import = "java.io.*,java.util.*" %>
<%@ page import = "javax.servlet.*,java.text.*" %>
<html>
<head>
<title>Display Current Date & Time</title>
</head>
<body>
<center>
<h1>Display Current Date & Time</h1>
</center>
<%
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>
Compile the above servlet once more, then call it using the URL http://localhost:8080/CurrentDate. As a result, you'll get the following:

Points to remember:

You can also read about the JSP Architecture and Lifecycle.
FAQs
-
What is the full form of JSP?
Jakarta Server Pages (JSP; formerly JavaServer Pages) is a collection of technologies that aids software developers in the creation of dynamically produced web pages using HTML, XML, SOAP, and other document types. Released in 1999 by Sun Microsystems, JSP is similar to PHP and ASP, but uses the Java programming language.
-
What are the advantages of JSP?
It's only for simple inclusions that can build connections and utilize form data. Database connections can also be included in JSP. It can hold any Java object. JSP has excellent performance and scalability because it allows dynamic elements embedded in HTML pages.
-
How can I get the current date in JSP?
Use the following code snippet:
Date date = new Date();
String date=date.toString();
Key Takeaways
We have learned Date Class in JSP, methods associated with date class, date comparison, and date formatting in this blog.
We bring to you more interesting blogs related to JSP. Click the links to read more:
Page Redirection in JSP.
Happy learning!