//TimerListenerを使って、自動的に現在時刻を表示更新するTickerを作成する。

import com.nttdocomo.ui.*;//IApplicationのiアプリで使う各種クラスの利用
import com.nttdocomo.util.*;//Timerの利用のため
import java.util.*;//Calendar利用

//用意されているComponentのサブクラス(Label、TextBoxなど)はfinalクラスでサブクラスを作ることができない。
//よって自作の別なクラス(GetTimeCls)を別途に作り任せる。

public class ClockTicker implements TimerListener{
	public Ticker myTicker = new Ticker("");
	Timer timer;
	
	public ClockTicker(){
  		timer = new Timer();		//タイマーオブジェクト生成
		timer.setListener(this);	//リスナーは自身に設定
		timer.setTime(1000);		//1秒ごと
		timer.setRepeat(true);		//タイマイベントを繰り返し発生
		timer.start();			//タイマを開始(timerExpiredが呼ばれ始まる)
	}
	
	public void timerExpired(Timer source){//TimerListenerのabstracを実装
		myTicker.setText(NowDay.getString());//ラベルに現在日時を設定
	}
	
	Ticker getClockTicker(){ //自身で管理するラベルを返す。
		return myTicker;
	}

	public void dispose(){ //このクラス利用者は、最後にこれで破棄しなければいけない!
		timer.stop();
		timer.dispose();
	}
}

//これはでは、通常のJavaアプリでも利用可能
class NowDay {
	static String W[] = {"日","月","火","水","木","金","土"};
	public static String getString(){
		String time = "";	//ラベル表示文字列
		Calendar now = Calendar.getInstance();//現在日時を取得
		
		int year = now.get(Calendar.YEAR);//年取得
		time += year + "年";
		
		int month = now.get(Calendar.MONTH);//月取得
		time += month + 1 + "月";
		
		int day = now.get(Calendar.DAY_OF_MONTH);//日取得
		time += day + "日";
		
		int week = now.get(Calendar.DAY_OF_WEEK);//週取得
		time += "(" + W[week-1] + ")";
		
		int hour = now.get(Calendar.HOUR_OF_DAY);//時間取得
		if ( hour < 10 ) time += "0";
		time += hour + ":";
		
		int minute = now.get(Calendar.MINUTE);//分間取得
		if ( minute < 10 ) time += "0";
		time += minute +":";
		
		int second = now.get(Calendar.SECOND);//秒間取得
		if ( second < 10 ) time += "0";
		time += second;
		return time;
	}
}



















SEO [PR] 爆速!無料ブログ 無料ホームページ開設 無料ライブ放送