Java program to print date and time

Created with Sketch.

 

Java program to print date and time

Java date and time program: Java code to print or display the current system date and time. We are using the GregorianCalendar class in our program. Don’t use Date and Time classes of java.util package as their methods are deprecated, i.e., they may not work in the future versions of JDK. As an alternative to the GregorianCalendar class, you can use the Calendar class.

Date and Time program in Java

import java.util.*;

class GetCurrentDateAndTime
{
public static void main(String args[])
{
int day, month, year;
int second, minute, hour;
GregorianCalendar date = new GregorianCalendar();

day = date.get(Calendar.DAY_OF_MONTH);
month = date.get(Calendar.MONTH);
year = date.get(Calendar.YEAR);

second = date.get(Calendar.SECOND);
minute = date.get(Calendar.MINUTE);
hour = date.get(Calendar.HOUR);

System.out.println(“Current date is  “+day+“/”+(month+1)+“/”+year);
System.out.println(“Current time is  “+hour+” : “+minute+” : “+second);
}
}

Output of program:
Java date and time program output

Leave a Reply

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