电子钟制作过程中出现的问题不能正确获取当前日期和时间,求解啊,运行和调试都没有出错,本来date()方法是无参的,但那时是错误的,后来加了0就没错误啦,但也没获
楼兰公子 2012-05-16 08:58:02 package test;
import java.sql.Date;
import java.sql.Time;
import java.text.SimpleDateFormat;
import java.util.Timer;
import java.util.concurrent.TimeUnit;
import java.sql.Timestamp;
import javax.xml.crypto.Data;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.internal.win32.SYSTEMTIME;
import org.eclipse.swt.widgets.Synchronizer;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.DateTime;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.ColorDialog;
/*
*@author hnlyxacj@163.com
*2012-5-16-上午1:21:56
*HelloSWT/test/Clock.java
*/
public class Clock {
public static void main(String[] args) {
// TODO Auto-generated method stub
final Display display = new Display();
final Shell shell = new Shell(display);
final Date curren_Date;
shell.setLayout(new FillLayout());
shell.setText("电子时钟");
shell.setSize(250, 150);
// 向Shell添加面板容器
final Composite composite = new Composite(shell, SWT.BORDER);
composite.setLayout(new FillLayout(SWT.VERTICAL));
// 向面板容器中添加两个标签
final Label ymd = new Label(composite, SWT.SINGLE | SWT.CENTER);
Font font = new Font(display, "华文新魏", 14, SWT.COLOR_WHITE);
ymd.setFont(font);
RGB rgb1 = new RGB(0, 0xff, 0);
ymd.setBackground(new Color(display, rgb1));
final Label hms = new Label(composite, SWT.SINGLE | SWT.CENTER);
hms.setBackground(new Color(display, rgb1));
hms.setFont(font);
shell.open();
// 创建新线程用于改变窗口的时间显示
new Thread(new Runnable() {
public void run() {
while (true) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
// TODO: handle exception
// 使用UI线程
display.asyncExec(new Runnable() {
@Override
public void run() {
[size=24px] Date date= new Date(0); // 定义显示时间的格式
SimpleDateFormat sdf1 = new SimpleDateFormat(
"yyyy年MM月dd日,E");
SimpleDateFormat sdf2 = new SimpleDateFormat(
"HH:mm:ss");
String ymdStr = sdf1.format(date);
String hmsStr = sdf2.format(date);
// 更改窗口中Label标签控件显示内容
ymd.setText(ymdStr);
hms.setText(hmsStr);
}
});
}
}
}).start();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
}
}