下面是一个实例,我吧主要的代码都放在这里了,可以看下:
public static Runnable refreshRunnable = new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
currentTime = new Date(System.currentTimeMillis());
while (SETTIME == true) {
hh = currentTime.getHours();
mm = currentTime.getMinutes();
ss = currentTime.getSeconds();
SETTIME = false;
}
currentTime.setHours(hh);
currentTime.setMinutes(mm);
currentTime.setSeconds(ss);
endTime = new Date(System.currentTimeMillis()
- currentTime.getTime() - 8 * 60 * 60 * 1000);
currentTime = endTime;
// 对系统当前时间进行格式化
String FormatcurrentDate = simpleDateFormat.format(currentTime);
callWindowText2.setText(FormatcurrentDate);
}
};
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
NewCallButton = (Button) findViewById(R.id.new_call);
NewCallButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
simpleDateFormat = new SimpleDateFormat(" HH:mm:ss");
handler = new Handler();
myThread = new Thread() {
public void run() {
while (ALWAYS == true && KEY_Dnow == true) {
handler.post(refreshRunnable);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
};
myThread.start();
}
});
由于System.currentTimeMillis()获取的时间是从8:00开始的,所以后面要减去它,
就可以实现一个从00:00:00开始计时的工具