×

Java Programs

Java Practice

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 »



Related Programs



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.