2. DATE & TIME
• Java provides the DATE class available in java.util package, this access the
current date and time
•This package implements Serialization, Cloneable and Comparable interface.
3. DATE CLASS
The class Date represents a specific instant in time, with millisecond
precision.
The list of major Date and Time classes:
java.util.Date class
java.util.Calendar class
4. java.util.Date
• Java provides the DATE class available in java.util package, this access the
current date and time.
• It uses some constructors and methods to represent date and time.
5. Constructor Description
Date() Creates a date object
representing current date and
time.
Date(long milliseconds) Creates a date object for the
given milliseconds
7. Methods Description
Methods Description
Boolean after(Date data) tests if current date is
after the given
Data.
Boolean before(Date date) tests if current date is
before the given date.
Object clone() returns the clone object of
current date.
Date Methods
8. Methods Description
Methods Description
int compareTo(Date date) compares current date
with given date.
Boolean equals(Date data) compares current date
with given date for
equality.
long getTime() returns the time
represented by this date
object.
Int hashcode() returns the hash code
value for this date object.
Date Methods
9. java Date after()
The after() method of Java Date class tests whether the date is after the
specified date or not.
Syntax:
public boolean after(Date )
12. java Date before()
The before() method of Java Date class tests whether the date is before the
specified date or not.
Syntax:
public boolean before(Date)
18. java Date compareTo()
The compareTo() method of Java Date class compares two dates and sort
them for order.
Syntax:
public boolean compareTo(Date anotherDate)
21. java Date equals()
The equals() method of Java Date class returns a Boolean value on the basis
of compression between two dates for equality. This method overrides
equals in class Object
Syntax:
public boolean equals(Object obj)
25. java Date getTime()
The getTime() method of Java Date class returns the number of
milliseconds represented by Date object.
Syntax:
public long getTime()
27. java Date hashCode()
The hashCode() method of Java Date class returns a value which is a hash
code for this object. This method overrides hashCode in class Object.
Syntax:
public int hashCode()
48. 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
//Predict the Output
import java.util.*;
public class Main
{
public static void main(String[] args)
{
Date d1 = new Date(12,4,2019);
Date d2 = new Date();
Date d3 = new Date(14,5,2020);
boolean a = d3.after(d1);
System.out.println("Date d3 comes after " +"date d2: " +
a);
boolean b = d3.before(d2);
System.out.println("Date d3 comes before "+ "date d2: " +
b);
int c = d1.compareTo(d2);
System.out.println(c);
System.out.println("Miliseconds from year to
date d1 is " + d1.getTime());
System.out.println("Before setting "+d2);
}
}
It provides constructors and methods to deal with date and time in java.
Package is nothing but a collection of classes.
Java util- it contain collection of framework legacy collection classes, event model, date and time facilities, internationalization, and miscellaneous utility classes.
Serialization-serialization converts the object into bytestream.
Cloneable-The object cloning is a way to create exact copy of an object
Description:
Date class is a class which is already a method inside the java.utill package.
It represents current date and time .
It uses some constructors and methods to represent date and time.
Description:
It uses some constructors and methods to represent date and time.
The java.util.Date class implements Serializable, Cloneable and Comparable<Date> interface.
Serialization-serialization converts the object into bytestream.
Cloneable-The object cloning is a way to create exact copy of an object.
Comparable-A comparable object is capable of comparing itself with another object. The class itself must implements the java.lang.Comparable interface to compare its instances.
Description:
This table mention as java.util.Date Constructor.
Output:
Wed Sep 18 05:06:59 UTC 2019
Description:
It will print the time in universal time pattern. Indian time slot is 5 hours ahead to universal time.
Import java.util.date -The java.util.Date class represents date and time in java.
Date object is created with constructor. class name and constructor name should be same.
Description:
It returns a Boolean value true if this date is after the specified date. Otherwise, returns false.
Output:
true
Description:
Date ‘d’ is after Date ‘d2’ so this output is true
Output:
true
true
Description:
Date d3=new Date() is mention as current date and time.
Date ‘d2’ is after Date ‘d’ so this output is true.
Date ‘d’ is after Date ‘d3’ so this output is true.
Description:
It returns a Boolean value true if this date is before the specified date. Otherwise, returns false.
Output:
true
Description:
Date 'd' is before Date 'd2‘ so this output is true.
Output:
true
true
Description:
In here date d4 is the current date
Date ‘d’ is before Date ‘d2’ so this output is true.
Date ‘d3’ is before Date ‘d4’ so output is true.
Output:
false
true
Description:
In here d4 is the current date
Date ‘d’ is not a before Date ‘d2’ so this output is false.
Date ‘d3’ is before Date ‘d4’ so output is true.
Description:
This method overrides clone in class Object.
It returns a clone/copy of this instance.
Output:
Thu Sep 17 07:03:39 UTC 2019
Description:
Clone of Date 'd‘.
Date d=new Date() is current date and time.
Description:
It returns the value 0 if the argument Date is equal to this Date.
It returns a value less than 0 if this Date is before the Date argument.
It returns a value greater than 0 if this Date is after the Date argument.
Output:
1
Description:
The d is compareTo d1 the before date is greater then d2 so output is 1.
In case, the date ‘d’ is less than date d2. the output will be -1.
Output:
0
Description:
Both b and b1 are equal date so it returns 0
Description:
It returns true if the objects are same otherwise false.
It returns a value less than 0 if this Date is before the Date argument.
It returns a value greater than 0 if this Date is after the Date argument.
Output:
true
Description:
Both b and b1 Dates are equals dates so it boolean method returns to ‘true’.
Output:
false
Description:
Both b and b1 Dates are not same dates so it boolean method returns to ‘true’.
Output:
true
Description:
Both b and b1 Current Dates are equals date so it boolean method returns to ‘true’.
It returns a value less than 0 if this Date is before the Date argument.
It returns a value greater than 0 if this Date is after the Date argument.
Output:
1568792698270
Description:
In this program getTime() method returns to current time in milliseconds.
Description:
It returns a hashCode value for this object.
It returns a value less than 0 if this Date is before the Date argument.
It returns a value greater than 0 if this Date is after the Date argument.
Output:
652671817
Description:
In this program hashCode method returns the object value Date class.
Description:
This program uses Java Date from() Method.
Syntax:
public static Date from(Instant instant)
It returns a date representing the same point on the timeline as the provided instant.
Section End/Start
Use this at section’s start or end. For example: “Questions” or “Time for Practice”. To be used for Impacts (get the student’s attention).
Descripion:
Java Date getDate()
Syntax:
public int getDate()
It returns the day of the month represented by this date object.
Output:
Current date
Descripion:
Java Date getHours()
Syntax:
public int getHours()
It returns the hours of the day represented by this date object.
Output:
Current hour.
Output:
Date d3 comes after date d2: true
Date d3 comes before date d2: true
-1
Miliseconds from year to date d1 is -1645574400000
Before setting Fri Sep 20 12:16:42 UTC 2019