×

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:

Grepcode Java util date

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:

Grepcode Java util date

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:

Grepcode Java util date

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:

Grepcode Java util date

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:

Grepcode Java util date

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:

Grepcode Java util date

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:

Grepcode Java util date

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:

Grepcode Java util date

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:

Grepcode Java util date

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:

Grepcode Java util date

Related Topics

Java Interface Lock

A synchronisation method called the Lock interface is available as of JDK 1.5. It is comparable to a synchronised block but more complex and versatile. The package java.util.concurrent contains the...

4 minutes read.

Minimum Number of Platforms Required for a Railway Station

The train station problem is one of the most significant problems typically posed in the programming round interview to gauge a candidate's aptitude for logic and problem-solving. Problem of Railway Station The...

6 minutes read.

Shift right zero Fill Operator in Java and Operator Shifting

Left shift operator ( << ) : The left shift operator is an operator which performs its action at the bits level of a binary Operator. That means when you perform...

4 minutes read.

Java Maven Silicon

Maven is a build automation tool that is mostly used for Java applications. It allows you to manage the build, reporting, and documentation of a project from the central location. Maven provides...

4 minutes read.

Highest precedence in Java

In Java, the operator is the first thing that springs to mind when discussing precedence. The order in which the operators in an expression are evaluated is controlled by a...

3 minutes read.

Access specifiers in Java

What are access specifiers in Java? Access specifiers in Java are used to determine whether other classes can use a particular field or invoke a particular method. Access specifiers mainly specify...

7 minutes read.

Java Do While Loop

When we wish to test the exit condition at the end of the loop, we use a do-while loop. The do-while loop always executes its body at least once, because...

1 minute read.

Kotlin Vs Java

Kotlin Vs Java There are many languages available for Android development. Java is the official language for android development but Kotlin is becoming popular nowadays. This article discusses both of these...

4 minutes read.

Advanced Java skills

These were some of the contemporary talents that a Java developer should master in efforts to progress in the era of science in 2022. A database such as MySQL, a...

4 minutes read.

Java Date add Days

In order to operate with the time and the Date in Java, we used the abstract Calendar class. It has several helpful interfaces that enable us to convert dates between...

4 minutes read.

Java Math signum() Method

The signum() method of Java Math class returns the signum function of the value. Syntax: public static double signum(double d)public static float signum (float d) Parameters: The parameter ‘d’ represents the floating-point value whose...

2 minutes read.

Factorial Program in Java using Recursion

Factorial Program in Java Factorials are used in mathematics to calculate permutations and combinations. It is denoted by the exclamatory symbol (!). Suppose, p is a number whose factorial is to...

3 minutes read.

Different Ways to Print Exception Message in Java

In this section, we will learn how to use various Java Throwable class methods to print exception messages. The Throwable class has the three methods listed below for printing the...

4 minutes read.

Java Short Keyword

Java supports eight different primitive datatypes. The language has predefined primitive datatypes that are given keyword names. Let's take a closer look at each of the eight primitive data types....

3 minutes read.

Java StringReader Class

StreamReader Class technique enables character reading from a string. The java.io package contains this class. A string serves as a source in the character stream. While the Stream class is...

4 minutes read.

Package naming convention in Java

It is conceivable that many programmers will use the same name for various types, given that Java programmers from all over the world create classes and interfaces. For illustration, suppose...

4 minutes read.

Java 8 Multimap

Java comes with several practical built-in collection libraries. However, there are situations when we need specialized collections that are not included in the Java standard library. The Multimap is one...

7 minutes read.

Hashtable in Java

Hashtable in Java The Hashtable class implements the Map interface and extends the Dictionary class. It implements a hash table which shows the key-value relation, i.e., it maps the keys to the values....

9 minutes read.

Java HashMap

Java HashMap extends AbstractMap and implements Map interface. It is the collection of multiple entries where an entry consists of key and value pair. The HashMap can contain only one...

7 minutes read.

Finding middle node of a linked list in Java

To find the middle node of a linked list we have various methods in Java. Method 1 In this method two pointers are used, one of which advances quickly, and the other of...

6 minutes read.