2个回答
展开全部
import java.util.Calendar;
import java.util.GregorianCalendar;
public class TimeDemo {
public static void main(String[] args) {
Thread thread = new Thread(new ReadTime());
thread.start();
}
}
class ReadTime implements Runnable {
// Calendar和GregorianCalendar
public void run() {
try {
while (true) {
Thread.sleep(1000);
getTimeUseCalendar();
getTimeUseGregorianCalendar();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public void getTimeUseCalendar() {
Calendar calendar = Calendar.getInstance();
System.out.println("Calendar time:" + calendar.get(Calendar.YEAR) + "-"
+ calendar.get(Calendar.MONTH) + "-"
+ calendar.get(Calendar.DAY_OF_MONTH) + " "
+ calendar.get(Calendar.HOUR_OF_DAY) + ":"
+ calendar.get(Calendar.MINUTE) + ":"
+ calendar.get(Calendar.SECOND));
}
public void getTimeUseGregorianCalendar() {
Calendar calendar = GregorianCalendar.getInstance();
System.out.println("GregorianCalendar time:"
+ calendar.get(Calendar.YEAR) + "-"
+ calendar.get(Calendar.MONTH) + "-"
+ calendar.get(Calendar.DAY_OF_MONTH) + " "
+ calendar.get(Calendar.HOUR_OF_DAY) + ":"
+ calendar.get(Calendar.MINUTE) + ":"
+ calendar.get(Calendar.SECOND));
}
}
展开全部
使用SimpleDateFormat格式化 calendar的getTime方法的返回值。
new
new SimpleDateFormat("yyyyMMdd hh:mm:ss").format(cal.getTime());
追问
初学java,不懂。。能帮我写出来了吗
追答
Calendar calendar = Calendar.getInstance();
SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd hh:mm:ss");
String r = formatter.format(calendar.getTime());
System.out.println(r);
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询