Site icon TestingDocs.com

Write a java program to display current Date and Time

Code Listing

Let’s write a simple java program that displays the current date and time on the console window.

package com.testingdocs.datetime;

//DateDemo.java
//www.TestingDocs.com

import java.util.GregorianCalendar;
import java.util.Calendar;

public class DateDemo {
    public static void main(String args[]) {
        GregorianCalendar date = new GregorianCalendar();
        int currentDay = date.get(Calendar.DAY_OF_MONTH);
        int currentMonth = date.get(Calendar.MONTH);
        int currentYear = date.get(Calendar.YEAR);
        int hour = date.get(Calendar.HOUR);
        int minute = date.get(Calendar.MINUTE);
        int second = date.get(Calendar.SECOND);
        System.out.println("Program Output----- ");
        System.out.println("Current Date := " +currentDay+ "/" +
(currentMonth+1)+ "/" +currentYear);
        System.out.println("Current Time := "+ hour + " : " +
 minute + " :" +second);
    }
}

Screenshot

Program Output

Program Output—–
Current Date := 5/8/2017
Current Time := 4 : 14 :12

Exit mobile version