Forum

Heyxu Forum

Forum Dictionary / Programming / 網頁/網站程式 / JavaScript /

Java 一段時間代碼 28800000 的問題?

New Subject
Random
Previous
Next
|
Java 一段時間代碼 28800000 的問題?

//顯示時間
Date d=new Date(System.currentTimeMillis()-startTime.getTime()-28800000);
這句話啥意思?為什麼-28800000?

下面是全部代碼:
import javax.swing.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.*;
import java.util.Date;
import java.text.SimpleDateFormat;

public class StopWatch extends JFrame {
JButton btnStart, btnStop;
JLabel label;
Timer timer;

public StopWatch() {
label = new JLabel("0000:00.000");
btnStart = new JButton("start");
btnStop = new JButton("stop");
final int delay = 100;
final Date startTime = new Date();
final SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss.S");
final Action taskPerformer = new AbstractAction() {
public void actionPerformed(ActionEvent evt) {
// 顯示時間
Date d = new Date(System.currentTimeMillis()
- startTime.getTime() - 28800000);
System.out.println(d);
label.setText(sdf.format(d));
label.repaint();
}
};
btnStart.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
startTime.setTime(new Date().getTime());
timer = new Timer(delay, taskPerformer);
timer.start();
}
});
btnStop.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
if (timer != null && timer.isRunning())
timer.stop();
}
});

Container c = getContentPane();
c.add(label, BorderLayout.NORTH);
c.add(btnStart, BorderLayout.CENTER);
c.add(btnStop, BorderLayout.SOUTH);
}

public static void main(String[] args) {

javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});

}

private static void createAndShowGUI() {
StopWatch window = new StopWatch();
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.pack();
window.setVisible(true);
}
}

  • Keywords : public, timer, import, label, window, startTime, final, btnStop, btnStart, StopWatch, ActionEvent, getTime, actionPerformed, System, JButton, BorderLayout, ActionListener, 28800000, 顯示, taskPerformer
0 0
2009-06-04T11:59:13+0000

雪彥 :

8 個小時的毫秒數
至於為什麼要這樣做 我也不是很清楚了
感覺是 1970 年 1 月 1 日 8 點
減掉 8 個小時吧
呵呵
0 0
2009-06-04T17:30:29+0000

魚戀秋波 Neil Y.K. :

28800000 是指秒數,相當於 8 個小時,而減掉 8 個小時的用意是因為我們所在的地理區域是中原的時間,而台灣地區是格林威治時間再加上 8 個小時。

這個機制一般運用在 Unix like 系統上,像是 Linux,因為 Unix like 所使用的 timestamp 是以格林威治時間記錄,所以當網頁從系統傳出時間給 Java 時,一般情況就必須減 8 個小時才是正常的時間。

如果網頁平台是 Window 視窗系統,就不必減 8 個小時,因為兩者運作機制不同,要注意一下唷。

0 0
2009-06-05T01:34:03+0000


  • Now, you can post comments by Facebook Account when your Heyxu account was logout or unvariable. whatever, we suggest to post comment by Heyxu Account to get more bounds to join new events of Heyxu
Comment
Verify
Comment
  • Auto Platform
    For better browsing experience, it will auto detects your device to display webpage.
  • Bug Report
    Tell us about the idea, problem and errors you found.
  • Copyright
    © 2009 Heyxu Multimedia, Inc - All rights reserved.
  • Revised Version
    V2.1.313
    02 Feb 2019 (GMT+8)