MOCKSTACKS
EN
Questions And Answers

More Tutorials









Java Date Class


Date class in java is available in java.util package.
This class provides the instant in time with precision of millisecond.

Constructors of the date class :


Date(): This constructor is used when we need an object of current date and time.
Date(long milliseconds): This constructor creates a date object from the number of milliseconds passed since January 1, 1970.
Example :
import java.util.*;
public class CWH extends Thread{
    public static void main(String[] args) {
        Date d= new Date();
        System.out.println("The current date is : " + d);

        Date d1= new Date(1621709639111l);
        System.out.println("The date calculated form miliseconds is : " + d1);

    }
}

Output

The current date is : Sun May 23 00:24:17 IST 2021
The date calculated form miliseconds is : Sun May 23 00:23:59 IST 2021

Methods of date class :


1. compareTo() :


Checks for the equality of the two dates.
Returns 0 if the dates are equal; else, returns 1.
Example :
import java.util.*;
public class CWH extends Thread{
    public static void main(String[] args) {
        Date d= new Date();

        Date d1= new Date(2021,12, 24);  //both dates are different
        System.out.println(d1.compareTo(d));
        
    }
}

Output

1

2. getTime() :


This method returns the number of milliseconds passed since the midnight of January 1, 1970.
Example :
import java.util.*;
public class CWH extends Thread{
    public static void main(String[] args) {
        Date d= new Date(2021,5,23);

        System.out.println("The number of milliseonds passed since Jan 1, 1970 :" +d.getTime() );

    }
}

Output

The number of milliseonds passed since Jan 1, 1970 :61582530600000

3. getYear() : Prints the current year.


getDate() : Prints the current date.
Example :
import java.util.*;
public class CWH extends Thread{
    public static void main(String[] args) {
        Date d= new Date();
        System.out.println("The current date is : "+ d.getDate());
        System.out.println("The current year is : "+ d.getYear()); //19

    }
}

Output

The current date is : 23
The current year is : 121

Example :


package com.company;

import java.util.Date;
public class cwh_97_date_class {
    public static void main(String[] args) {
//        System.out.println(Long.MAX_VALUE);
//        System.out.println(System.currentTimeMillis());
        Date d = new Date();
        System.out.println(d);
        System.out.println(d.getTime());
        System.out.println(d.getDate());
        System.out.println(d.getSeconds());
        System.out.println(d.getYear()); 
    }
}


Conclusion

In this page (written and validated by ) you learned about Java Date Class . What's Next? If you are interested in completing Java tutorial, your next topic will be learning about: Java Calendar Class.



Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue: mockstacks@gmail.com.


Share On:


Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using Mockstacks.com, you agree to have read and accepted our terms of use, cookies and privacy policy.