Grepcode Java util date
What is java.util.Date Class?
The date and time in Java are provided through the java.util.Date class.
If you imported java.util, it could be helpful. Use the Java.util.Date class to implement this class in your code. The current date and time can be used with the constructors and methods provided by this class.
import java.util.Date;
What do the constructors for java.util.Date do?
There are two main constructors for the java.util.Date class, which are explained here.
Date()
First Java.util. Date is the Date function Object(). The object is initialised using the event's time and date.
Date date = new Date();
In this case, we set the current date and time as the initial values for a date variable of type Date.
DateProgram.java
import java.util.Date;
public class DateProgram {
public static void main(String[] args) {
Date date = new Date();
System.out.println(date);
}
}
Output:

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.
long ms = System.currentTimeMillis();
Date date = new Date(ms);
Only after obtaining the specific milliseconds that have occurred so far in this thorough System had we populated the date variable with the current date and time System. currentTimeMillis(); and giving the function Object() an argument.
DateProgram.java
import java.util.Date;
public class DateProgam {
public static void main(String[] args) {
long ms = System.currentTimeMillis();
Date date = new Date(ms);
System.out.println(date);
}
}
Output:

What are the methods in java.util.Date?
Here are several crucial java.util.Date techniques.
1. boolean after(Date date): If the current date is earlier than the specified date, this function returns true.
2. boolean before(Date date): if the date being returned is earlier than the specified date, returns true.
3. int compareTo(Date date): compares the supplied date to the present day.
4. boolean equals(Date date): analyses the similarity of the provided date to the current one. It returns true if they are identical.
5. long getTime(): This date object's time is returned.
6. void setTime(long time): updates the current time to the specified time.
7. String toString(): creates a String type object from this date.
DateProgram.java
import java.util.Date;
public class DateProgram {
public static void main(String args[]) {
long ms = 1000000;
Date dateA = new Date(ms);
System.out.println("dateA : " + dateA);
Date dateB = new Date();
System.out.println("dateB : " + dateB);
boolean after = dateB.after(dateA);
System.out.println("Is dateB subsequent to dateA : " + after);
boolean before = dateB.before(dateA);
System.out.println("Is dateB earlier than dateA : " + before);
}
}
Output:

Explanation
Date variables date1 and date2 are defined in the code above. Then, the techniques date2.after(date1) and date2.before(date1) were used. Date2 arrives after Date1. Hence the after() Method returns true. Date2 does not come before Date1. Hence the before() function returns false.
DataAfterProgram.java
import java.util.Date;
public class DateAfterProgram {
public static void main(String[] args) {
Date d=new Date(2018,9,21);
Date d2=new Date(1997,3,10);
System.out.println("Date 'd' occurs after Date 'd2' : "+d.after(d2));
}
}
Output:

DateBeforeProgram.java
import java.util.Date;
public class DateBeforeProgram {
public static void main(String[] args) {
Date d=new Date(1990,3,10);
Date d2=new Date(2022,9,22);
System.out.println("Date 'd' is before Date 'd2' : "+d.before(d2));
}
}
Output:

DateEqualsProgram.java
import java.util.Date;
public class DateEqualsProgram {
public static void main(String[] args) {
Date d=new Date(2022,9,22);
Date d1=new Date(1990,3,10);
System.out.println("Date 'd' equals Date 'd1' : "+d.equals(d1));
}
}
Output:

DataGetTimeProgram.java
import java.util.Date;
public class DateGetTimeProgram {
public static void main(String[] args) {
Date d=new Date(1990,3,10);
System.out.println("Current number of milliseconds science January 1, 1970, 00:00:00 GTM : "+d.getTime());
}
}
Output:

DataSetTimeProgram.java
import java.util.Date;
public class DateSetTimeProgram {
public static void main(String[] args) {
Date d=new Date(1990,3,10);
long l=3000;
d.setTime(l);
System.out.println("time after configuration : "+d.toString());
}
}
Output:

StringProgram.java
// Java application to demonstrate the
//Method java.lang.String.toString()
import java.io.*;
public class StringProgram {
public static void main(String args[])
{
String Strobj = new String("How are you?");
System.out.print("Hello : ");
System.out.println(Strobj.toString());
}
}
Output:

DateProgram.java
import java.util.Date;
public class DateProgram {
public static void main(String[] args) {
Date a = new Date(2022, 4, 24);
Date b = new Date(2022, 6, 21);
Date c = new Date(2022, 4, 22);
int output = a.compareTo(b);
System.out.println("If date1 comes after date2: " + output);
output = b.compareTo(c);
System.out.println("If date2 comes before date3: " + output);
output = a.compareTo(c);
System.out.println("If date1 equal to date3: " + output);
}
}
Output:
