因为,有时我们需要动态获取系统的当前时间,然后进行比对。做出相应的判断。目前我想到的方法是使用线程的方法进行系统时间的动态获取。因为不能等着事件的发生,然后再去取系统时间,这样是不合适的。比如:要对“发送”按钮的做延迟。你点的很快,但要求职发送一个。就可以用这种方法。
package test.thread;
import java.util.Date;
import java.text.SimpleDateFormat;
class getSysTimeThread extends Thread {
private String threadName;
public getSysTimeThread(String str) {
threadName = str;
System.out.println("当前线程:" + threadName);
}
public void run() {
while (true) {
long currentTime = System.currentTimeMillis();
System.out.println("time: " + currentTime);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
new getSysTimeThread("current time").start();
}
}
package test.thread;
import java.util.Date;
import java.text.SimpleDateFormat;
class getSysTimeThread extends Thread {
private String threadName;
public getSysTimeThread(String str) {
threadName = str;
System.out.println("当前线程:" + threadName);
}
public void run() {
while (true) {
long currentTime = System.currentTimeMillis();
System.out.println("time: " + currentTime);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
new getSysTimeThread("current time").start();
}
}