You are here

Java program to print date and time

Java date and time program: Java code to print or display current system date and time. We are using 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 be supported in the future versions of JDK. As an alternative of GregorianCalendar class you can use Calendar class.

Java programming code

  1. import java.util.*;
  2.  
  3. class GetCurrentDateAndTime
  4. {
  5.    public static void main(String args[])
  6.    {
  7.       int day, month, year;
  8.       int second, minute, hour;
  9.       GregorianCalendar date = new GregorianCalendar();
  10.      
  11.       day = date.get(Calendar.DAY_OF_MONTH);
  12.       month = date.get(Calendar.MONTH);
  13.       year = date.get(Calendar.YEAR);
  14.      
  15.       second = date.get(Calendar.SECOND);
  16.       minute = date.get(Calendar.MINUTE);
  17.       hour = date.get(Calendar.HOUR);
  18.  
  19.       System.out.println("Current date is  "+day+"/"+(month+1)+"/"+year);
  20.       System.out.println("Current time is  "+hour+" : "+minute+" : "+second);
  21.    }
  22. }      

Output of program:
Java date and time program output

Download Date and time program class file.