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

0
575

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:

[java]

import java.util.Date;

public class CurrentSystemDate{
public static void main(String args[]) {
// Instantiate a Date object
Date date = new Date();

// display time and date using toString()
System.out.println(date.toString());
}
}

[/java]

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

Facebook Comments

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.