- 相關(guān)推薦
java怎樣給時(shí)間格式化
【提要】本篇《java如何給時(shí)間格式化》特別為需要格式編程學(xué)習(xí)的朋友收集整理的,僅供參考。內(nèi)容如下:
java中如何格式化的時(shí)間,這是一個(gè)很簡(jiǎn)單的問(wèn)題,在實(shí)際的編程中經(jīng)常用,以下是小編為大家搜索整理的java如何給時(shí)間格式化,希望能給大家?guī)?lái)幫助!更多精彩內(nèi)容請(qǐng)持續(xù)關(guān)注我們考試網(wǎng)!
日期格式化
import java.util.Date;
import java.text.SimpleDateFormat;
class dayTime
{
public static void main(String args[])
{
Date nowTime=new Date();
System.out.println(nowTime);
SimpleDateFormat time=new SimpleDateFormat("yyyy MM dd HH mm ss");
System.out.println(time.format(nowTime));
}
}
public void timerStop(){
if(timer!=null)
timer.cancel();
}
public static void main(String[] args){
BugXmlTimer myTimer=new BugXmlTimer();
// TODO Auto-generated method stub
myTimer.timerStart();
}
}
//這是執(zhí)行任務(wù)的類,即每隔一段時(shí)間要做的事情在這里
package com.my.time;
import java.util.TimerTask;
public class BugXmlTimerTask extends TimerTask {
@Override
public void run() {
System.out.print("run task");
}
}
//以下是出發(fā)定時(shí)操作的類,該類實(shí)現(xiàn)了ServletContextListener
public class MyTimerListener implements ServletContextListener {
private BugXmlTimer mytimer = new BugXmlTimer ();
public void contextInitialized(ServletContextEvent event) {
mytimer.timerStart();
}
public void contextDestroyed(ServletContextEvent event) {
mytimer.timerStop();
}
}
然后在web.xml里部署一下,即可在程序啟動(dòng)后運(yùn)行定時(shí)器了!
com.my.time.MyTimerListener
【java怎樣給時(shí)間格式化】相關(guān)文章:
java如何給時(shí)間格式化08-13
java格式化輸出的方法09-07
怎樣格式化內(nèi)存卡08-13
怎樣保護(hù)硬盤不被格式化10-24
Java 的日期時(shí)間08-21
php格式化時(shí)間戳的方法技巧10-28
Java獲取UTC時(shí)間的方法08-09
java垃圾回收機(jī)制是怎樣的05-21