ITSourceCode.com

We Exist to Provide 100% Free Source Code and Tutorials

Custom Search

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

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

Check Your Domain Ranking

Leave a Reply

Your email address will not be published. Required fields are marked *

ITSourceCode.com © 2016 Frontier Theme

Subscribe For Latest Updates

Signup for our newsletter and get notified when we publish new articles for free!