How to get current date and time of the system in Java

0
179

In Java we have Date class available in java.util package which encapsulates the current date and time.

The Date class have two constructors mentioned below:

1 Date( )
This constructor initializes the object with the current date and time. This doesn’t require any argument.

2 Date(long millisec)
We need to provide an argument to this constructor that equals the number of milliseconds that have elapsed since midnight, January 1, 1970

Below is a program to get current date and time:
It is very easy to get current date and time in Java. We need to create Date object with toString() method to print current date and time as follows:

  1. import java.util.Date;
  2.  
  3. public class CurrentSystemDate{
  4. public static void main(String args[]) {
  5. // Instantiate a Date object
  6. Date date = new Date();
  7.  
  8. // display time and date using toString()
  9. System.out.println(date.toString());
  10. }
  11. }

OUTPUT:
on February 03 10:01:47 CDT 2016

Facebook Comments
(Visited 245 times, 1 visits today)

LEAVE A REPLY

Please enter your comment!
Please enter your name here