ADVERTISEMENT
ADVERTISEMENT

Java program to get current system date and time

Using Date Class:

By using Date class in Java you can get the current date and time of the system. You have to just create instance of the Date class and print it in string format.

Get Date and Time program in Java

//program to get system date and time
import java.util.Date;

public class GetDateTime {
  public static void main(String args[]) {
    // instance of Date class
    Date date = new Date();

    // get date, month and year
    System.out.println(date.getDate() + "/" + (date.getMonth() + 1) + "/" + (date.getYear() - 100));

    // get complete date and time
    System.out.println(date.toString());

    // get time only
    System.out.println(date.getHours() + ":" + date.getMinutes() + ":" + date.getSeconds());
  }
}

Output:

    28/4/16
    Thu Apr 28 22:40:44 IST 2016
    22:40:44

Core Java Example Programs »


ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions


ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.