package 实验13;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class 距离元旦之电子时钟 {
public static void main(String[] args) {
newYearDate y = new newYearDate();
Thread th = new Thread(y,"元旦");
th.start();
}
}
class newYearDate implements Runnable{
JFrame jf = new JFrame("元旦放假倒计时");
JPanel jp = new JPanel();
JPanel jp2 = new JPanel();
JPanel jp3 = new JPanel();
JLabel jl = new JLabel();
JLabel jl2 = new JLabel();
public newYearDate() {
jp2.setLayout(new GridLayout(2, 1));
jp3.add(jl2);
jp2.add(jp3);
jp3.setBackground(Color.BLACK);
jp2.add(jp);
jl2.setText("距离元旦还有:");
jl.setFont(new Font("微软雅黑", Font.BOLD, 30));
jl.setForeground(Color.RED);
jl2.setFont(new Font("微软雅黑", Font.BOLD, 20));
jl2.setForeground(Color.RED);
jp.add(jl);
jp.setBackground(Color.BLACK);
jf.add(jp2);
jf.setBounds(300, 300, 1000, 200);
jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
@Override
public void run() {
// TODO 自动生成的方法存根
while(true) {
String time = getSurplusDate_2();
//System.out.println(time);
jl.setText(time);
jp2.validate();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static String getSurplusDate_2() {
String str = "";
Calendar newYear = Calendar.getInstance();
newYear.set(2019, 0, 1, 0, 0, 0); //注意一定要把时分秒设置上 否则的话时间会一直固定
Calendar cal = Calendar.getInstance();
long time = newYear.getTimeInMillis() - cal.getTimeInMillis();
time /= 1000;
long day = 0,hour = 0,min = 0,second = 0;
day = time / (24 * 60 * 60);
hour = time / (60 * 60) - day * 24;
min = time / (60) - day * 24 * 60 - hour * 60;
second = time - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60;
// str = day + "天 " + hour + ":" + min + ":" + second;
Calendar temp = Calendar.getInstance();
temp.set(Calendar.HOUR, (int)hour);
temp.set(Calendar.MINUTE, (int)min);
temp.set(Calendar.SECOND, (int)second);
SimpleDateFormat sdf = new SimpleDateFormat("hh:mm:ss");
str = day + "天" + sdf.format(temp.getTime());
return str;
}
}