/*
* GregorianCalendar日期类的使用
*/
package com.date;
import java.util.*;
public class mydate {
public static void main(String[] args){
timer timer = new timer();
Thread t = new Thread(timer);
t.start();
}
}
class timer implements Runnable{
@Override
public void run() {
// TODO Auto-generated method stub
while(true){
try {
Thread.sleep(1000);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
this.printTime();
}
}
public void printTime(){
/*Date date = new Date();
System.out.println(date);*/
Calendar calendar = null;
calendar = new GregorianCalendar();
String year = String.valueOf(calendar.get(Calendar.YEAR));
String month = String.valueOf(calendar.get(Calendar.MONTH)+1);
String day = String.valueOf(calendar.get(Calendar.DAY_OF_MONTH));
String hour = String.valueOf(calendar.get(Calendar.HOUR_OF_DAY));
String minute = String.valueOf(calendar.get(Calendar.MINUTE));
String second = String.valueOf(calendar.get(Calendar.SECOND));
String millisecond = String.valueOf(calendar.get(Calendar.MILLISECOND));
System.out.println(year+"年"+month+"月"+day+"日 "+hour+"时:"+minute+"分:"+second+"秒"+"微秒:"+millisecond);
}
}
转载于:https://blog.51cto.com/6846041/1353818