JSP Current Date and Time
JSP Date Handling
All methods of core Java can be used in JSP is the biggest advantage of JSP.
In this section, we will be using Date class of java.util package, and it consists of date and time.
It supports two constructors:
Date() – It gives us current date and time
Date(long miilsec) – This takes parameter of milliseconds which has been elapsed on January 1 1970
Boolean after(Date date) – It gives after date of the given date parameter
Boolean before(Date date) – It gives the before date of the given date parameter
Object clone() – It creates a copy of the date object
IntcompareTo(Date date) – it compares the date class object with another one
IntcompareTo(Object date) – it compares with the object class object with another one
Boolean equals(Object date) – it checks whether two date objects are equal
Long getTime() – it fetches the time
InthashCode() – it fetches the hashcode of the given date
Void setTime(long Time) – It sets the time of given date object
String toString() – It converts into string object of date object.
Example:
In this example, we are fetching the current date and time using date object
<%@ 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>Guru current Date</title> </head> <body> Today's date: <%= (new java.util.Date()).toLocaleString()%> </body> </html>
Explanation of the code:
Code Line 11: We are using date object to fetch the current date and time.
When you execute the above code, you get the following output
Output:
We are getting current date and time