A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

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());  
}
}

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马