© 2014 Firstsoft Technologies (P) Limited. login
Hi 'Guest'
Home SiteMap Contact Us Disclaimer
Quick Links
Easy Studies

Displaying the Current Time in browser Using Java Applet


This program represents the applet code to get the system time. This program shows the current time. This type of program is used to display the time on browser where your application is running on. This example displays the time in an applet in the time format like: hours, minutes and then seconds (hh:mm:ss). Here, the ClockApplet is a class name extends from the Applet class and implements to the Runnable interface. The applet class is embedded with html to run this on the browser.

Displaying the Current Time in browser Using Java Applet :

import java.applet.*; import java.awt.*; import java.util.*; public class ClockApplet extends Applet implements Runnable{ Thread t,t1; public void start(){ t = new Thread(this); t.start(); } public void run(){ t1 = Thread.currentThread(); while(t1 == t){ repaint(); try{ t1.sleep(1000); }catch(InterruptedException e){} } } public void paint(Graphics g){ Calendar cal = new GregorianCalendar(); String hour = String.valueOf(cal.get(Calendar.HOUR)); String minute = String.valueOf(cal.get(Calendar.MINUTE)); String second = String.valueOf(cal.get(Calendar.SECOND)); g.setColor(Color.red); g.drawString(hour + ":" + minute + ":" + second, 20, 30); } }

Sample ScreenShot:

SLogix Student Projects

⇓Student Projects⇓
⇑Student Projects⇑