Monday, January 13, 2014

Write A Java Program To Get Current Date and Time

/************************************************************
Program        Write A Java Program To Get Current Date and Time
*************************************************************/



/*-----------------<< Program  >>-------------------*/
import java.util.*;

class DateAndTime
{
   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  >>-------------------*/


Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.

C:\Documents and Settings\JABIR>cd\

C:\>cd C:\Program Files\Java\jdk1.6.0_04\bin

C:\Program Files\Java\jdk1.6.0_04\bin>javac DateAndTime.java

C:\Program Files\Java\jdk1.6.0_04\bin>java DateAndTime

Current Date is -  13/1/2014

Current Time is  -  5 : 11 : 32




No comments:

Post a Comment