Java用Calendar如何每秒取一次系统时间
4个回答
展开全部
用线程可以啊
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();
}
}
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();
}
}
展开全部
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;
}
}
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;
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
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;
}
}
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;
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
1楼可以为
Calender aa = Calender.getI...;
Date d = aa.getTime();
system.out.print(d);
Calender aa = Calender.getI...;
Date d = aa.getTime();
system.out.print(d);
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询