Java Date class

Introduction

Java Date class represents the current date and time.

Date supports the following non-deprecated constructors:

Date()  
Date(long millisec) 

A simple demo.


// Show date and time using only Date methods.
import java.util.Date;

public class Main {
  public static void main(String args[]) {
    // Instantiate a Date object
    Date date = new Date();
    /*from  w  w  w .j a  va 2  s.  c  om*/
    // display time and date using toString()
    System.out.println(date);

    // Display number of milliseconds since midnight, January 1, 1970 GMT
    long msec = date.getTime();
    System.out.println("Milliseconds since Jan. 1, 1970 GMT = " + msec);
  }
}

To obtain more-detailed information about the date and time, use the Calendar class.




PreviousNext

Related