×

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

Difference Between BufferedReader and FileReader

BufferedReader: To read data from a specified character stream, two classes are used: buffered readers and file readers. Both of them have advantages and disadvantages. Although how they operate is the...

6 minutes read.

Java Math min() Method

The min() method of Math class returns the smaller of two arguments. The arguments can be of double, float, int or long data type. Syntax: public static double min (double a, double...

2 minutes read.

Java Naming Conventions

JAVA NAMING CONVENTIONS Java naming convention is a standard pattern for writing your identifier name such as class, interfaces, methods, constants, variable, etc. These patterns are not standard rules that you must...

2 minutes read.

Stone Game in Java

In this tutorial, we will learn to design a stone game in Java. First of all, we will understand what is this game all about. We will grasp it through...

9 minutes read.

Figurate Number in Java

There have been several uses for figurate or figural numerals throughout history. A number that may be expressed by regular, distinct geometric shapes with spaced evenly points is referred to...

4 minutes read.

How to check if date is valid in Java?

In this article, you will acknowledge about how to verify if a date is valid or not. For this you will learn the approach, you will be able to write...

3 minutes read.

Java Set Interface

We use set when we don't want to allow duplicate entries. All Set implementations do not allow duplicates. HashSet: This class stores its elements in hash tables. It uses the hashCode()...

3 minutes read.

Java Vs C++

Java Vs C++ Java and C++ both are Object Oriented Programming languages. Both languages are popular for competitive programming. C++ is used by many coders who have just started learning programming...

4 minutes read.

Ad Hoc Problem on Arrays in Java

Ad hoc problems are issues that arise unexpectedly and require immediate attention. These problems can range from small and straightforward issues to more complex and time-consuming problems. Examples of ad...

3 minutes read.

Java Math atan() Method

The atan() method of Math class computes the trigonometric Arc Tangent (inverse of tangent ) of an angle. The value returned is between -pi/2 to pi/2. Syntax: public static double atan(double a) Parameters: The...

2 minutes read.

Java String endsWith() method

Checks whether this String ends with the specified suffix or not. Syntax public boolean endsWith(String Suffix) Parameter Suffix : It takes suffix as a parameter  i.e. Sequence of characters Returns It returns true when the current...

1 minute read.

How to find length of integer in Java

We can find the length of the integer in many ways. The length of an integer is defined as the count of the number of digits for the given integer. These...

5 minutes read.

Basic Terms in Multithreading

To understand the terms of multithreading, we must have knowledge about concurrency, processes, and threads. Concurrency The concurrency stands for performing multiple tasks at the same time. In the process communication, the operating system permits the process...

8 minutes read.

Least Operator to Express Number in Java

In this article, we will learn about how to obtain a target number using a single number or a single integer by leveraging least operators in Java. There can be...

3 minutes read.

Java Full Stack

A person who can create both the front end and back end of an application is a full-stack developer. In essence, the term "Java full-stack" refers to a web developer...

9 minutes read.

Best Practices to use String Class in Java

Use String Builder or String Buffer for String concatenation in place of + operator.Compare two strings by equals( ) method instead == operator.Call .equals( ) method on a known String...

3 minutes read.

Java String hashCode() method

Java String hashCode() method returns hash code for current String. hash code for string object is computed as s[0]*31^(n - 1) + s[1]*31^(n - 2) + ... + s[n - 1] Using int...

2 minutes read.

Uses of Java

Java is used in many real-world Java applications, including technologies and tools. This Java programming language has become the backbone for developing many applications. In areas like embedded systems and...

3 minutes read.

Matrix Multiplication Program in Java

Matrix Multiplication Program in Java The matrix multiplication program in Java is the continuation of the matrix program in Java that we have already discussed earlier. In this section, we will...

3 minutes read.

Java Integer sum() method

The sum() method of Java Integer class add the two specified integers values. It returns the same result as given by + operator. Syntax public static int sum (int a, int b)  Parameters The...

1 minute read.