Java如何每秒取一次系统时间
答案:4 悬赏:60 手机版
解决时间 2021-01-25 17:31
- 提问者网友:杀手的诗
- 2021-01-25 05:32
取出来的系统时间format成String类型就行!
最佳答案
- 五星知识达人网友:慢性怪人
- 2021-01-25 05:52
使用Timer,或者TimerTask实现每一妙执行一次任务,再读出现在的时间,然后使用SimpleDateFormat格式化一下就OK了
全部回答
- 1楼网友:不甚了了
- 2021-01-25 08:29
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Timer;
public class MainTest {
public static void main(String[] args) {
Timer timer=new Timer();
MyTimerTask task=new MyTimerTask();
timer.schedule(task, new Date(), 1000);
}
}
class MyTimerTask extends java.util.TimerTask {
String now;
public void run() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
now=sdf.format(new Date());
System.out.println(now);
}
public String getNow(){
return this.now;
}
}
- 2楼网友:神也偏爱
- 2021-01-25 08:06
用线程可以啊
import java.util.*;
public class test implements runnable{
thread th0 ;
//public void run(){}
public test(thread th)
{
th0 = th;
th0= new thread(this);
th0.start();
}
public void run()
{
try{
while(true)
{
if(thread.currentthread()==th0){
calendar cal = calendar.getinstance();
int wd = cal.get(calendar.day_of_week);
string x = "";
switch(wd){
case 1:x="星期日";break;
case 2:x="星期一";break;
case 3:x="星期二";break;
case 4:x="星期三";break;
case 5:x="星期四";break;
case 6:x="星期五";break;
case 7:x="星期六";break;
}
int y = cal.get(calendar.year);
int mon = cal.get(calendar.month)+1;
int d = cal.get(calendar.date);
int h = cal.get(calendar.hour);
int min = cal.get(calendar.minute);
int s = cal.get(calendar.second);
int ap = cal.get(calendar.am_pm);
if(ap==1)
h = h+12;
stringbuffer sb = new stringbuffer("日期:"+y+"-"+((mon<=9)?"0"+mon:mon)+"-"+((d<=9)?"0"+d:d)+" 时间:"+h+":"+((min<=9)?"0"+min:min)+":"+((s<=9)?"0"+s:s)+" "+x);
system.out.println(sb);
thread.sleep(1000);
}
}
}catch(exception ee){
ee.getstacktrace();
}
}
public static void main(string[] args) {
thread th = new thread();
test t = new test(th);
t.run();
}
}
- 3楼网友:詩光轨車
- 2021-01-25 06:40
import java.text.SimpleDateFormat;
import java.util.Calendar;
public class TestTime {
public static void main(String[] args){
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String time = sdf.format(new java.util.Date());
System.out.println("现在时间是:"+ time);
}
}
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯