Relógio não atualiza

3 respostas
denisspitfire

Pessoal pq o relogio nao atualiza?
Segue o código

public class Principal {
	public static void main(String[] args) {
		Date data = new Date();
		Calendar cal = new GregorianCalendar();
		for (int i = 0; i < 500; i++) {
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			System.out.println(cal.getTime());
		}
	}
}

3 Respostas

R

o getTime vai somente obter valor que foi gerado no momento em que o seu calendar foi criado.

denisspitfire

vlw.

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class Principal {
	public static void main(String[] args) {
		Date data = new Date();
		Calendar cal;
		for (int i = 0; i < 500; i++) {
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			cal = new GregorianCalendar();
			System.out.println(cal.getTime());
		}
	}
}
denisspitfire

Agora é outra situação, a hora atualiza mas escreve por cima.

public class Principal extends Frame {
	public static void main(String[] argv) {
		Principal h = new Principal();
	}

	public Principal() {
		setTitle("Hora");
		setSize(300, 250);

		Panel hello = new Panel();
		add("Center", hello);

		Button button = new Button("OK");
		add("South", button);
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				System.exit(0);
			}
		});

		setVisible(true);

		int width = hello.getWidth();
		int height = hello.getHeight();
		Graphics g = hello.getGraphics();
		horaAtual(g, width, height);

		addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				System.exit(0);
			}
		});
	}

	private void horaAtual(Graphics g, int width, int height) {
		// TODO Auto-generated method stub
		Calendar cal;
		while (true) {
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			cal = new GregorianCalendar();
			g.drawString(" ", width / 2 - 25,
					height / 2);
			g.drawString("" + cal.getTime(), width / 2 - 25,
					height / 2);
		}
	}
}
Criado 25 de março de 2013
Ultima resposta 25 de mar. de 2013
Respostas 3
Participantes 2