Home  • Programming • Java

Java program example to display current date and time

Code
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);
   }
}
Compile and Run: Don't use Date and Time class of java.util package as their methods are deprecated means they may not be supported in future versions of JDK. As an alternative of GregorianCalendar class you can use Calendar class.

Comments 0


Share

Copyright © 2024. Powered by Intellect Software Ltd