Grepcode java.util.Date

What does the java.util.Date Class does?

The java.util.Date class in Java provides the date and time.

It might be advantageous if we imported java.util. If we want to implement these classes in our code, use Java.util.Date class. The constructors as well as methods offered by this class allow for the use of the current time and day.

Why java.util.Date constructors used for?

The Date class has two major constructors, which are described below.

Date()

The Date function instance is the first component of Java.util (). The event's date and time are used to initialize the object.

Here, we initialize a date variable of type using the current time and date.

File Name: DP.java

Output:

Sat Jan 14 18:51:49 IST 2023

Date(long milliseconds)

The milliseconds since January 1, 1970, 00:00:00 GMT, are calculated using this java.util.Date function Object() to generate a date object.

We only added the current time and day System to the date variable after receiving the precise milliseconds that had already passed in this thorough system. currentTimeMillis(); and providing an input to the method Object().

File name: DP1.java

Output:

Sat Jan 14 18:58:41 IST 2023

What is the java.util.Date methods?

Here is a few essential java.util.Date strategies.

  1. boolean after(Date date): The function will return true if the current date seems earlier than the supplied date.
  2. boolean before(Date date): It returns true if the date being supplied is earlier than that of the desired date.
  3. int compareTo(Date date): It compares the given date with the current time.
  4. boolean equals(Date date): It evaluates how closely the given date resembles the current one. If they match, it returns true.
  5. long getTime(): The time of this date object is displayed.
  6. void setTime(long time): The requested time is added to the current time.
  7. String toString(): It creates an object of the type String using this date.

File Name: DP2.java

Output:

dateA is : Thu Jan 01 05:46:40 IST 1970
 dateB is : Sat Jan 14 19:52:21 IST 2023
 Is dB after the dA : true
 Is dB before the dA : false

Explanation

The code above defines the date variables date1 as well as date2. Then, date2.after(date1) and date2.before(date1) procedures were applied. Date2 follows Date1 in time. The after() Method, therefore, returns true. Date2 is not earlier than Date1. As a result, before() returns false.

File name: DataAfter.java

Output:

Is date 'do' occur after the date 'd1'? true

File name: DateBefore.java

Output:

Is date 'd0' occurs before the date 'd1': true

File name: DateEquals.java

Output:

Is date 'd0' equals to date 'd1' : false

File name: DataGetTime.java

Output:

The present number of milliseconds since January 1, 1971, 00:00:00 GTM : 6059736000000

File name: DataSetTime.java

Output:

After configuration, the time is: Thu Jan 01 00:00:03 GMT 1970

File name: StringCode.java

Output:

Hello : Welcome to JavaTpoint

File name: DateCode.java

Output:

Is date1 coming after the date2: -1
Is date2 coming before the date3: 1
Is date1 is equal to date3: -1