Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
Getting the Current Time 
 

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

 

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.

                         

» View all related tutorials
Related Tags: c string reference io method type boolean key rmi ole this ai node oo create if boo example to contains

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

1 comments so far (
post your own) View All Comments Latest 10 Comments:

This code i have learnt and applied in my project but its nt getting the system time. When i run it ordinary it got the sys time but when i applied it on a gui it didnt work.
This is a thread.

public void dateTime()
{
datetimeThread = new Thread(this);

datetimeThread.start();
}

public void run()
{
while(datetimeThread != null)
{
display();
try
{
datetimeThread.sleep(1000);
}
catch(InterruptedException e)
{
showStatus("Thread interrupted");
}
}
}

public void display()
{
date = new Date();
calendar = new GregorianCalendar();
calendar.setTime(date);
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";
}
strTime = ("Current Time : " + hour + ":" + minute + ":" + second + " " + am_pm);
strDate = (calendar.get(Calendar.MONTH)+1)+"/"+calendar.get(Calendar.DATE)+"/"+calendar.get(Calendar.YEAR);
strStatus=strTime+" "+"Current Date " + strDate;
showStatus(strStatus);
}

Posted by Emeka Nwankwo on Sunday, 11.30.08 @ 02:34am | #82191

Training Courses
Tell A Friend
Your Friend Name
Software Solutions
Least Viewed
Most Rated
Recently Viewed
Search Tutorials

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.