Tutorials

Java Date


java.util.Date


java.util.Date class provides methods and constructors to work with Date and Time in java.

Date class implements following interface:

  • Serializable interface
  • Cloneable interface
  • Comparable interface

Example 1 on java.util.Date:


package Date;

import java.util.Date;

public class DateDemo {

      public static void main(String[] args) {

            Date today = new Date();
            System.out.println(today);

      }

}

OUTPUT:

Thu Dec 21 10:09:53 IST 2017

 

Example 2 on java.util.Date:


package Date;

import java.util.Date;

public class DateDemo {

      public static void main(String[] args) {
           
            long dateMillis = System.currentTimeMillis();
            Date today = new Date(dateMillis);

            System.out.println(today);
      }

}

 

OUTPUT:

Thu Dec 21 10:09:53 IST 2017

 

java.util.Date Constructors

From the above two example, we can see two constructors in action:

  1. Date today = new Date()
  2. Date today = new Date(dateMillis)

Date() ⇒ This constructor create date instance to represent current date and time.

Date(dateMillis) ⇒ This constructor takes time in milliseconds as parameter and create date instance to represent current date and time.

 



Please Share this page
Views : 11