Pr.Pg Next Pg

Handling Date tutorials

  • One of the most important advantages of using JSP is that you can use most of the methods available in core Java. 

  • In this lesion we will use Date class available in a java.util package, this class encapsulates the current date and time.

  • It is very easy to display current date and time on our browser.

  • There are two constructors supported by Date class as below:

  1. Date(): This constructor initializes the object with the current date and time.

  2. Date(long millisec): This constructor accepts one argument that equals the number of milliseconds that have elapsed since midnight, January 1, 1970.

  • Below is the list of Date object methods with description:

 

SN

Methods

Description

1

boolean after(Date date)

Checks whether this date comes after the specified date.

2

boolean before(Date date)

Checks whether this date comes before the specified date.

3

Object clone( )

Duplicates the invoking Date object.

4

int compareTo(Date date)

Compares the value of the invoking object with that of date. Returns 0 if the values are equal. Returns a negative value if the invoking object is earlier than date. Returns a positive value if the invoking object is later than date.

5

int compareTo(Object obj)

Operates identically to compareTo(Date) if obj is of class Date. Otherwise, it throws a ClassCastException.

6

boolean equals(Object date)

Compares this object against the specified object.

7

long getTime( )

Returns the time in milliseconds since the epoch. January 1,1970

8

int hashCode( )

Returns a hash code for the invoking object.

9

void setTime(long time)

Sets the time and date as specified by time, which represents an elapsed time in milliseconds from midnight, January 1, 1970

10

String toString( )

Converts a date to a String, using the UNIX ctime conventions.

 


 

JSP program to get current date and time

Sam Sir

<%-- Program name DateDemo.jsp--%>

<%-- JSP program to get current date and time --%>

<%@page import="java.util.Date" contentType="text/html" pageEncoding="UTF-8"%>

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>JSP Page</title>

</head>

<body>

<h4>System Date Demo in JSP</h4>

<hr>

<%

Date d =new Date();

%>

Today is <b><%= d %></b>

</body>

</html>

Output

Date time

Pr.Pg border                                              Next Pg