timecountdown.java

来自「简单的奥运倒计时牌子」· Java 代码 · 共 65 行

JAVA
65
字号
/**
 * <p>Title:TimeCountDown</p>
 * <p>Discription:自己的程序,提供倒计时秒数和现在时间</p>
 * @author DELL
 * @version 1.0
 */
package Project1;

import java.text.SimpleDateFormat;
import java.util.*;
import javax.swing.*;

public class TimeCountDown extends JPanel implements Runnable{
//之后再试验下在这个位置定义全局变量
	Thread clocker = new Thread();
	JLabel now = new JLabel();
	JLabel left = new JLabel();
	public int CountDay(){
		int day = 0;
		Calendar rightnow = Calendar.getInstance();
		int year = rightnow.get(Calendar.YEAR);
		if (year<2008){
			day = (2008 - year)*365 - rightnow.get(Calendar.DAY_OF_YEAR) +220;
		}else{
			day = 220 - rightnow.get(Calendar.DAY_OF_YEAR);
		}
		return day;
	}
	public long CountSeconds(){
		long seconds = 0;
		Calendar rightnow = Calendar.getInstance();
		int day = CountDay();
		seconds = (day+1)*86400 
		- rightnow.get(Calendar.HOUR_OF_DAY)*3600
		- rightnow.get(Calendar.MINUTE)*60
		- rightnow.get(Calendar.SECOND);
		return seconds;
	}
	

	public void run(){
		
		
		while (true) {
			Date nw = new Date();
			SimpleDateFormat time = new SimpleDateFormat(
					"yyyy-MM-dd hh:mm:ss aaa");
			now.setText(time.format(nw));
			left.setText(new Long(CountSeconds()).toString());
			try {
				clocker.sleep(1000);
			}
			catch (InterruptedException ie) {
				System.err.print(ie);
			}
		}
	}
	public static void main(String[] args){
		TimeCountDown time = new TimeCountDown();
		System.out.println(time.CountDay());
		System.out.println(time.CountSeconds());
		time.run();
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?