Write an Applet which display current date and time on the web page??

Write an Applet which display current date and time on the web page??

View Answers

July 9, 2012 at 2:24 PM

Here is an applet code that display date and time.

import java.applet.*;
import java.awt.*;
import java.util.*;
import java.text.*;

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();
  Date d=cal.getTime();
  SimpleDateFormat sdf=new SimpleDateFormat("dd-MM-yyyy hh:mm:ss");
  g.drawString(sdf.format(d), 20, 30);
  }
}

To call this applet, you need to create an html file.

<HTML>
<BODY>
<div align = "center">
<APPLET CODE = "ClockApplet" WIDTH = "500" HEIGHT = "400"></APPLET>
</div>
</BODY>
</HTML>









Related Tutorials/Questions & Answers:
Advertisements