黑马程序员技术交流社区

标题: 利用线程同步系统时间的万年历 [打印本页]

作者: fuwei52331314    时间: 2016-7-6 18:19
标题: 利用线程同步系统时间的万年历
package Thread类;
import java.util.Calendar;
import java.util.Date;
public class TimeDemo {
public static void main(String[] args) throws InterruptedException {
  while(true){
   Thread.sleep(1000);
   new Thread(new Time()).start();
  }
}
}
class Time implements Runnable{
@Override
public void run() {
  Calendar c = Calendar.getInstance();
  c.setTime(new Date());
  StringBuffer sb = new StringBuffer("系统时间:");
  int y = c.get(Calendar.YEAR);
  int m = c.get(Calendar.MONTH);
  int d = c.get(Calendar.DATE);
  int h = c.get(Calendar.HOUR_OF_DAY);
  int mm = c.get(Calendar.MINUTE);
  int s = c.get(Calendar.SECOND);
  
  String hour = "0";
  String min = "0";
  String se = "0";
  if(h < 10){
   hour += h;
  }else{
   hour = h + "";
  }
  if(mm < 10){
   min += mm;
  }else{
   min = mm + "";
  }
  if(s < 10){
   se += s;
  }else{
   se = s + "";
  }
  sb.append(y).append("年")
  .append(m).append("月")
  .append(d).append("日")
  .append(hour).append("时")
  .append(min).append("分")
  .append(se).append("秒");
  
  System.out.println(sb.toString());  
}
}






欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2