×

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

Chromatic Number in Java

The chromatic number is the bare minimum of colours necessary to accurately colour any graph. To put it another way, the chromatic number can be thought of as the bare...

6 minutes read.

Mutable class in Java

A language for object-oriented programming is Java. Because this is an object-oriented language of programming, all of its mechanisms and methods are based on objects. Java has a concept of...

6 minutes read.

How to Read CSV Files in Java?

Comma-Separated Values is the acronym for this format. It is a straightforward file format storing textual tabular data in a database or spreadsheet. The CSV files can be imported into...

3 minutes read.

How to run java program in ubuntu

To run the java programming in ubuntu, we need to follow several steps: Let's see the process. Step1: Install the Java compiler or JDK to the system. To run the java program, we...

3 minutes read.

Java String equals() method

equals() method compares two string based on the their content. Syntax public boolean equals(Objects anObject) parameter anObject: Object to be compared with the current String. Returns It returns true when the current String is equivalent to...

1 minute read.

Bean class in Java

The web applications that are created with the help of the JSP or Java Server Pages generally have fairly more functionality than the web pages that specifically are created with...

5 minutes read.

Java Pi

What is Pi? There are many formulas in geometry that employ the Pi constant to calculate things like circumference, area, and volume. A circle's circumference divided by its diameter yields a...

3 minutes read.

Packages Program in java

Let us know what package is in the Java programming language, and later we will learn about types of packages and how to define a package. How to import Java...

4 minutes read.

Java Session

Session indicates interval of time. A session is a simple  time interval in which servers and client interacts. To maintain the state of the client or user we use technologies...

5 minutes read.

Sudoku in Java

Sudoku is a combinatorial-number-placement puzzle with a logic-based approach. The goal of a traditional Sudoku puzzle is to fill in the numbers on a 9 by 9 grid so that...

6 minutes read.

Java float vs double

Java : Java is a pure object oriented language. It was introduced by James Gosling in the year 1995. The first public implementation of java was done by sun micro systems...

7 minutes read.

Replace character in string Java

Characters in Java In the package of Java language, there is a container class called Character. A single field of type char is contained in a Character object. For manipulating characters,...

4 minutes read.

Java Generate Random String

Java Generate Random String In this tutorial, we will learn about to generate random string in Java. Random generation strings mean any string will be generated, which does not follow any...

7 minutes read.

Java String valueOf() method

Java String valueOf() method converts different types of values into String. Such as : int to String, long to String, boolean to String, character to String, float to String, double to...

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

How to calculate time difference in Java

In this tutorial, we learned about how to calculate time differences in java language and which methods use to find the difference and its example. Prerequisite To understand this example, you need...

5 minutes read.

Static() Function in Java

The static keyword in Java is suitable for variables, constants, and functions. The static keyword is mainly used to control storage so that it may be appropriately used. We shall...

3 minutes read.

Convert Char array to string in java

A collection of characters is referred to as a string. A character array differs from a string in that the string is canceled by the special character "\0." A string...

4 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 HashSet

HashSet implements the set interface. It uses the hash table to make the collection to store different data types. The hash set is the unordered collection of different data types....

6 minutes read.