Getting the Current Time

This section shows the way of getting the current time of the system.

Ads

Tutorials   
How to read properties file in Java? Generate random numbers in Java Cloneable Interface in Java What is the flaw with the Stack class? List In Java Java Stack Example HashSet In Java Enumeration in java How to generate random number in java Iterator in java Queue in java Transient Java Keyword clone method in Java Java Queue example Logger in Java Java current date - How to get current date in Java Collection : LinkedList Example Collection : ArrayList Example Random alpha numeric string of fixed size Java List Iterator Example The Hashtable Class The BitSet class Java Vector Java Enumeration Data Structures in Java Java Transient Variable Java Date Format Example Java Properties File Example Map.Entry Java Interface Java Property File java preferences,Change the value of Preferences by user Retrieving a Preference Node Java hasNext Write Keys and Values to the Properties file in Java Get All Keys and Values of the Properties files in Java Properties file in Java Properties file in Java Determining If a Preference Node Contains a Specific Value Change the value of Preferences by user Clone method example in Java Implementing a Simple Event Notifier Determining When a Preference Node Is Added or Removed Listening for Changes to Preference Values in a Preference Node Importing Preferences Finding a Preference in a Preference Tree Retrieving the Parent and Child Nodes of a Preference Node Determining If a Preference Node Exists Removing a Preference Node Creating a Preference Node Getting the Maximum Size of a Preference Key and Value
Ads

Getting the Current Time

     

This section shows the way of getting  the current time of the system.

In this section, through the given program you can get the current time in the proper format (Hour:Minute:Second AM/PM). This program shows the current time by using the various methods of the Calendar class. These are explained as follows:

Code Description:

Calendar:
Date is the special instance of the time. This class helps you to convert a date format object into the integer fields like YEAR, MONTH, HOUR, MINUTE, SECOND etc.

Calendar.get(Calendar.MINUTE):
Above method gives you the minute value from the current time and MINUTE is the predefined field of the Calendar class which returns the integer value i.e. the minute value of the current time

Calendar.AM_PM:
Above field of the Calendar class returns the integer value 1, if the current time is in the PM otherwise it returns 0 value which denotes the time is in the AM period.

Here is the code of the program:

import java.util.*;

public class CurrentTime{
  public static void main(String[] args){
  Calendar calendar = new GregorianCalendar();
  String am_pm;
  int hour = calendar.get(Calendar.HOUR);
  int minute = calendar.get(Calendar.MINUTE);
  int second = calendar.get(Calendar.SECOND);
  if(calendar.get(Calendar.AM_PM== 0)
  am_pm = "AM";
  else
  am_pm = "PM";
  System.out.println("Current Time : " + hour + ":" 
+ minute + ":" + second + " " + am_pm);
  }
}

Download this example.

Advertisements

Ads
Share on Google+Share on Google+

Getting the Current Time

Posted on: April 16, 2007 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Advertisements

Ads
 

Discuss: Getting the Current Time   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:6
Aleksandar
April 12, 2011
Getting the Current Time

Hi! I added this line: int month = calendar.get(Calendar.MONTH); and what I got was one month less than the actual month. Why is it so?
Rajesh
August 24, 2011
Request

need online Run for all programs...
MsObond
July 19, 2012
dynamic clock

what about a program that shows a dynamic clock , where the seonds are actually increasing on the same spot.
ganesh dudhade
August 21, 2012
about error

int hour = calendar.get(Calendar.HOUR); incorrect this is correct int hour = calendar.get(Calendar.HOUR_OF_DAY);
Mandla
November 12, 2012
Count down clock

How to creat a count down program that shuts down my computer.
JavaNub
January 6, 2013
Help

I made this code: import java.text.SimpleDateFormat; import java.util.*; import javax.swing.*; public class TimeBox{ public static void main(String[] args) { Calendar calendar = new GregorianCalendar(); int hour = calendar.get(Calendar.HOUR); int minute = calendar.get(Calendar.MINUTE); int second = calendar.get(Calendar.SECOND); JOptionPane.showMessageDialog("Current Time", hour + ":" + minute + ":" + second); } } And I get an error. y u no work?

Ads

 

Ads