0% found this document useful (0 votes)
5 views49 pages

18-Date and Time-28-09-2024

The document provides an overview of the Java Date class from the java.util package, detailing its constructors, methods, and functionality for handling date and time. It explains key methods such as after(), before(), clone(), compareTo(), equals(), getTime(), and hashCode(), along with example code snippets to illustrate their usage. Additionally, it includes multiple-choice questions to test understanding of the concepts presented.

Uploaded by

prajwal sri tej
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views49 pages

18-Date and Time-28-09-2024

The document provides an overview of the Java Date class from the java.util package, detailing its constructors, methods, and functionality for handling date and time. It explains key methods such as after(), before(), clone(), compareTo(), equals(), getTime(), and hashCode(), along with example code snippets to illustrate their usage. Additionally, it includes multiple-choice questions to test understanding of the concepts presented.

Uploaded by

prajwal sri tej
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 49

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.
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
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.


Constructor Description

Date() Creates a date object


representing current date
and time.

Date(long milliseconds) Creates a date object for


the given milliseconds
1 //Predict the Output
2 import java.util.Date;
3 public class Main
4 {
5 public static void main(String args[])
6 {
7 Date date = new Date();
8 System.out.print(date);
9 }
10 }
11
12
13
14
15
16
17
18
19
20
21
22
Methods Description
Date Methods
Methods Description
Boolean after(Date tests if current date is
data) after the given
Data.
Boolean before(Date tests if current date is
date) before the given date.
Object clone() returns the clone object
of current date.
Methods Description
Date Methods
Methods Description
int compareTo(Date compares current date
date) with given date.
Boolean equals(Date compares current date
data) 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.
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 )
1 //Predict the Output
2 import java.util.Date;
3 public class Main
4 {
5 public static void main(String args[])
6 {
7 Date d=new Date(1996,10,19);
8 Date d2=new Date(1995,11,20);
9 System.out.println(d.after(d2));
10 }
11 }
12
13
14
15
16
17
18
19
20
21
22
1 //Predict the Output
2 import java.util.Date;
3 public class Main
4 {
5 public static void main(String args[])
6 {
7 Date d=new Date(1993,10,19);
8 Date d2=new Date(1996,11,20);
9 Date d3=new Date();
10 System.out.println(d2.after(d));
11 System.out.println(d.after(d3));
12 }
13
14 }
15
16
17
18
19
20
21
22
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)
1 //Predict the Output
2 import java.util.Date;
3 public class Main
4 {
5 public static void main(String args[])
6 {
7 Date d=new Date(2015,3,10);
8 Date d2=new Date(2018,9,21);
9 System.out.println(d.before(d2));
10 }
11
12 }
13
14
15
16
17
18
19
20
21
22
1 //Predict the Output
2 import java.util.Date;
3 public class Main
4 {
5 public static void main(String args[])
6 {
7 Date d=new Date(2015,3,10);
8 Date d2=new Date(2018,9,21);
9 Date d3=new Date(17,9,2018);
10 Date d4=new Date();
11 System.out.println(d.before(d2));
12 System.out.println(d3.before(d4));
13 }
14 }
15
16
17
18
19
20
21
22
1 //Predict the Output
2 import java.util.Date;
3 public class Main
4 {
5 public static void main(String args[])
6 {
7 Date d=new Date(2019,11,10);
8 Date d2=new Date(2018,9,21);
9 Date d3=new Date(9,19,2018);
10 Date d4=new Date();
11 System.out.println(d.before(d2));
12 System.out.println(d3.before(d4));
13 }
14 }
15
16
17
18
19
20
21
22
java Date clone()
The clone() method of Java Date class returns a copy/clone of this
object.

Syntax:
public boolean clone()
1 //Predict the Output
2 import java.util.Date;
3 public class Main
4 {
5 public static void main(String args[])
6 {
7 Date d=new Date();
8 System.out.println(d.clone());
9 }
10 }
11
12
13
14
15
16
17
18
19
20
21
22
java Date compareTo()
The compareTo() method of Java Date class compares two dates
and sort them for order.
Syntax:
public boolean compareTo(Date anotherDate)
1 //Predict the Output
2 import java.util.Date;
3 public class Main
4 {
5 public static void main(String args[])
6 {
7 Date d=new Date(2020,3,10);
8 Date d1=new Date(2018,5,21);
9 int compare=d.compareTo(d1);
10 System.out.println(compare);
11 }
12 }
13
14
15
16
17
18
19
20
21
22
1 //Predict the Output
2 import java.util.Date;
3 public class Main
4 {
5 public static void main(String args[])
6 {
7 Date d=new Date();
8 Date d1=new Date();
9 int compare=d.compareTo(d1);
10 System.out.println(compare);
11 }
12 }
13
14
15
16
17
18
19
20
21
22
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)
1 //Predict the Output
2 import java.util.Date;
3 public class Main
4 {
5 public static void main(String args[])
6 {
7 Date d=new Date(12,06,2019);
8 Date d1=new Date(12,06,2019);
9 boolean compare=d.equals(d1);
10 System.out.println(compare);
11 }
12 }
13
14
15
16
17
18
19
20
21
22
1 //Predict the Output
2 import java.util.Date;
3 public class Main
4 {
5 public static void main(String args[])
6 {
7 Date d=new Date(12,06,2019);
8 Date d1=new Date(5,06,2019);
9 boolean compare=d.equals(d1);
10 System.out.println(compare);
11 }
12 }
13
14
15
16
17
18
19
20
21
22
1 //Predict the Output
2 import java.util.Date;
3 public class Main
4 {
5 public static void main(String args[])
6 {
7 Date d=new Date();
8 Date d1=new Date();
9 boolean compare=d.equals(d1);
10 System.out.println(compare);
11 }
12 }
13
14
15
16
17
18
19
20
21
22
java Date getTime()
The getTime() method of Java Date class returns the number of
milliseconds represented by Date object.

Syntax:
public long getTime()
1 //Predict the Output
2 import java.util.Date;
3 public class Main
4 {
5 public static void main(String args[])
6 {
7 Date d=new Date();
8 System.out.println(d.getTime());
9 }
10 }
11
12
13
14
15
16
17
18
19
20
21
22
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()
1 //Predict the Output
2 import java.util.Date;
3 public class Main
4 {
5 public static void main(String args[])
6 {
7 Date d=new Date(1996,3,10);
8 System.out.println(d.hashCode());
9 }
10 }
11
12
13
14
15
16
17
18
19
20
21
22
MCQ
1 //Predict the Output
2 import java.util.Date;
3 public class JavaDateAfterExample2
4 {
5 public static void main(String[] args)
6 {
7 Date d=new Date(29,5);
8 Date d2=new Date(22,3);
9 System.out.println(d.after(d2));
10 }
11 }
12
13
14
15
16
17
18
19
20
21
22
Question 1
A false
)

B No output
)

C true
)

D Compilation error
)
Question 1
A false
)

B No output
)

C true
)

D Compilation error
)
1 //Predict the Output
2 import java.util.Date;
3 public class JavaDateAfterExample2
4 {
5 public static void main(String[] args)
6 {
7 Date d=new Date();
8 Date d1=new Date(23,5,1998);
9 System.out.println(d.clone());
10 }
11 }
12
13
14
15
16
17
18
19
20
21
22
Question 2
A 23,5,1998
)

B Print current date and time


)

C Print Current date only


)

D Compilation error
)
Question 2
A 23,5,1998
)

B Print current date and time


)

C Print Current date only


)

D Compilation error
)
1 //Predict the Output
2 import java.util.Date;
3 public class Main
4 {
5 public static void main(String[] args)
6 {
7 Date d=new Date(23,05,1345);
8 System.out.println(d.clone(d));
9 }
10 }
11
12
13
14
15
16
17
18
19
20
21
22
Question 3
A Current date and time
)

B No output
)

C 23,5,1345
)

D Compilation error
)
Question 3
A Current date and time
)

B No output
)

C 23,5,1345
)

D Compilation error
)
1 //Predict the Output
2 import java.util.Date;
3 import java.time.Instant;
4 public class JavaDateAfterExample2
5 {
6 public static void main(String[] args)
7 {
8 Date d=new Date();
9 Instant i= Instant.now();
10 System.out.println(d.from(i));
11 }
12 }
13
14
15
16
17
18
19
20
21
22
Question 4
A Current date and time
)

B No output
)

C Exception
)

D Compilation error
)
Question 4
A Current date and time
)

B No output
)

C Exception
)

D Compilation error
)
1 //Predict the Output
2 import java.util.Date;
3 public class Main
4 {
5 public static void main(String[] args)
6 {
7 Date date=new Date();
8 Date date1=new Date(1997,3,10);
9 int comparison=date.compareTo(date1);
10 System.out.print(comparison);
11 }
12 }
13
14
15
16
17
18
19
20
21
22
Question 5
A -1
)

B 1
)

C 0
)

D Compilation error
)
Question 5
A -1
)

B 1
)

C 0
)

D Compilation error
)
Programming
1 //Predict the Output
2 import java.util.Date;
3 public class Main
4 {
5 public static void main(String[] args)
6 {
7 Date d=new Date();
8 System.out.println(d.getDate());
9 }
10 }
11
12
13
14
15
16
17
18
19
20
21
22
1 //Predict the Output
2 import java.util.Date;
3 public class Main
4 {
5 public static void main(String[] args)
6 {
7 Date d=new Date();
8 System.out.println(d.getHours());
9 }
10 }
11
12
13
14
15
16
17
18
19
20
21
22
1 //Predict the Output
2 import java.util.*;
3 public class Main
4 {
5 public static void main(String[] args)
6 {
7 Date d1 = new Date(12,4,2019);
8 Date d2 = new Date();
9 Date d3 = new Date(14,5,2020);
10 boolean a = d3.after(d1);
11 System.out.println("Date d3 comes after " +"date d2: " +
12 a);
13 boolean b = d3.before(d2);
14 System.out.println("Date d3 comes before "+ "date d2: " +
15 b);
16 int c = d1.compareTo(d2);
17 System.out.println(c);
18 System.out.println("Miliseconds from year to
19 date d1 is " + d1.getTime());
20 System.out.println("Before setting "+d2);
21 }
22 }
THANK YOU

You might also like