util.date class methods in Java with Examples

util.date class methods


Following are some important date class methods :

    .toString() :java.util.Date.tostring() method is a java.util.Date class method.It displays the Current date and time.
    Here Date object is converted to a string and represented as:

day mon dd hh:mm:ss zz yyyy

day : day of the week
mon : month
dd : day of the month
hh : hour
mm : minute
ss : second
zz : time zone
yyyy : year upto 4 decimal places

Syntax: public String toString() Return: a string representation of the given date.
Syntax: public void setTime(long time) Parameters: time : the number of milliseconds.
Syntax: public int hashCode() Return: a hash code value for the Date object.
Java Code to illustrate the use of .toString(), setTime(), hashCode() methods. // Java Program explaining util.date class methods// // use of .toString(), setTime(), hashCode() methods import java.util.*; // class having access to Date class methods public class NewClass public static void main(String[] args) Date mydate = new Date(); // Displaying the current date and time System.out.println( "System date : " + mydate.toString() ); // Is used to set time by milliseconds. Adds 15680 // milliseconds to January 1, 1970 to get new time. mydate.setTime( 15680 ); System.out.println( "Time after setting: " + mydate.toString()); int d = mydate.hashCode(); System.out.println( "Amount (in ms) by which time" + " is shifted : " + d); Output of Java code:
System date : Tue Nov 01 02:37:18 IST 2016 Time after setting: Thu Jan 01 05:30:15 IST 1970 Amount (in milliseconds) by which time is shifted : 15680
Syntax: public boolean after(Date d) Parameters: d : date Return: true if and only if the instant represented by this Date object is strictly later than the instant represented by 'when'; else false Exception: NullPointerException - if Date object is null.
Syntax: public Object clone() Return: a clone of this instance.
Syntax: public boolean before(Date d) Parameters: d : date Return: true if and only if the instant represented by this Date object is strictly earlier than the instant represented by 'when'; else false Exception: NullPointerException - if when is null.

Java Code to illustrate the use of after(), clone(), before() methods.