java中如何完成倒计时

zhjyym_java 2009-07-03 02:28:40
各位,小弟提些很无知的问题,怎么在java中提取系统时间,时间能相互加减吗?
我准备做一个倒计时,望各位多多指教。
...全文
205 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
阿彪兄 2009-07-04
  • 打赏
  • 举报
回复
支持 一下
kadach11 2009-07-03
  • 打赏
  • 举报
回复
不明白你为何要在JAVA里做倒计时?计算显示的话用javascript就Ok,需要存储用数据库也行。
yxc0433 2009-07-03
  • 打赏
  • 举报
回复
Date过时了,用Calendar吧
不善^ 2009-07-03
  • 打赏
  • 举报
回复
刚网上看倒的

public class 倒计时时钟 {

//小时
private int hours;

//分钟
private int min;

//秒
private int second;

public 倒计时时钟(int hours,int min,int second) {
this.hours = hours;
this.min = min;
this.second = second;
}

public int getHours() {
return hours;
}

public void setHours(int hours) {
this.hours = hours;
}

public int getMin() {
return min;
}

public void setMin(int min) {
this.min = min;
}

public int getSecond() {
return second;
}

public void setSecond(int second) {
this.second = second;
}

}




public class 倒计时 implements Runnable{

private 倒计时时钟 clock;
private long time;

public 倒计时(倒计时时钟 clock) {
this.clock = clock;
//将时间换算成秒
time = clock.getHours()*60*60+clock.getMin()*60+clock.getSecond();
}

public void run() {
while(time >= 0) {
try {
Thread.sleep(1000);
time -= 1;//时间减去一秒
clock.setHours((int)time/(60*60));
clock.setMin((int)(time/60)%60);
clock.setSecond((int)time % 60);

} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//return clock;
}

public 倒计时时钟 getTime() {
return clock;
}

public static void main(String [] args) {
倒计时时钟 clock = new 倒计时时钟(0,10,0);

倒计时 jishi = new 倒计时(clock);
显示 show = new 显示(jishi.getTime());
//显示 show = new 显示();
new Thread(show).start();
new Thread(jishi).start();

}
}

class 显示 implements Runnable {
private 倒计时时钟 clock;
public 显示(倒计时时钟 clock) {
this.clock = clock;

}

public void run() {

while(clock.getHours() != 0 ||
clock.getMin() != 0 ||
clock.getSecond() != 0) {
try {
System.out.println(String.format("%02d",clock.getHours())+
":"+String.format("%02d",clock.getMin())+
":"+String.format("%02d",clock.getSecond()));
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}
}


zhjyym_java 2009-07-03
  • 打赏
  • 举报
回复
如果用Date的话,是有这个Date(int year, int month, int day)构造方法吗
小李子 2009-07-03
  • 打赏
  • 举报
回复
System.currentTimeMillis()
或 new Date获取当前时间
时间可以加减运算

62,615

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧