android单选定时器,Android 定时器

public classTimerUtil {private static final String TAG = "TimerUtil";private static Handler mTimeHandler = null;private static boolean mTimerMark = false;private static Runnable mTimerRunnable = null;private static int mUpdateTime = 0;privateTimerUtil() {

};/*** 定时器开始定时

*

*@paramdoThing 定时器处理事情

*@paramupdateTime 定时器时间

*@sinceV1.0*/

public static void startTime(Runnable doThing, intupdateTime) {if (null == doThing || updateTime < 0) {return;

}if (null ==mTimeHandler) {

mTimeHandler= newHandler();

mTimerRunnable=doThing;

mUpdateTime=updateTime;

mTimerMark= true;

}if(mTimerMark) {

mTimeHandler.postDelayed(mTimerRunnable,0);

}else{

mTimeHandler.postDelayed(null, 0);

}

}/*** 定时器开始定时

*

*@paramdoThing 定时器处理事情

*@paramupdateTime 定时器时间

*@parammode 定时器模式

*@sinceV1.0*/

public static void startTime(Runnable doThing, int updateTime, booleanmode) {if (null == doThing || updateTime < 0) {return;

}if (null ==mTimeHandler) {

mTimeHandler= newHandler();

mTimerRunnable=doThing;

mUpdateTime=updateTime;

mTimerMark= true;

}if(mTimerMark) {if(mode) {

mTimeHandler.postDelayed(mTimerRunnable,0);

}else{

mTimeHandler.postDelayed(mTimerRunnable, updateTime);

}

}else{

mTimeHandler.postDelayed(null, 0);

}

}/*** 这里对方法做描述

*

*@sinceV1.0*/

public static voidupdataTime() {if(mTimerMark) {

mTimeHandler.postDelayed(mTimerRunnable, mUpdateTime);

}else{

mTimeHandler.postDelayed(null, 0);

}

}/*** 这里对方法做描述

*

*@sinceV1.0*/

public static voidstopTime() {

mTimerMark= false;if (null !=mTimeHandler) {

mTimeHandler.removeCallbacks(mTimerRunnable);

mTimeHandler= null;

}

}/*** time格式 String型的日 如:昨天、今天、8月24日 15:30

*@paramltime

*@paramcontext

*@return*@sinceV1.0*/

public synchronized static String getTime_FormatTime_MMDD(longltime, Context context) {

String time= "";

Calendar dateCalendar=Calendar.getInstance();

dateCalendar.setTimeInMillis(ltime);

Calendar targetCalendar=Calendar.getInstance();

targetCalendar.set(Calendar.HOUR_OF_DAY,0);

targetCalendar.set(Calendar.MINUTE,0);if(dateCalendar.after(targetCalendar)) {

time= context.getString(R.string.today) +" "+time;returntime;

}else{

targetCalendar.add(Calendar.DATE,-1);if(dateCalendar.after(targetCalendar)) {

time= context.getString(R.string.yesterday) +" "+time;returntime;

}

}

String otherSDF=context.getString(R.string.mmdd);

Date date=dateCalendar.getTime();

SimpleDateFormat sfd= newSimpleDateFormat(otherSDF);

time=sfd.format(date);

CLog.d(TAG,"getTime_FormatTime_MMDD time:"+time);returntime;

}/*** time格式 String型的日 如:昨天、今天、8月24日 15:30

*@paramltime

*@paramcontext

*@return*@sinceV1.0*/

public synchronized static String getTime_FormatTime_MMDDHHMM(longltime, Context context) {

String time= "";

Calendar dateCalendar=Calendar.getInstance();

dateCalendar.setTimeInMillis(ltime);int month = dateCalendar.get(Calendar.MONTH) + 1;int day =dateCalendar.get(Calendar.DAY_OF_MONTH);int hour =dateCalendar.get(Calendar.HOUR_OF_DAY);int minute =dateCalendar.get(Calendar.MINUTE);

time= String.format("%02d:%02d", hour, minute);

Calendar targetCalendar=Calendar.getInstance();

targetCalendar.set(Calendar.HOUR_OF_DAY,0);

targetCalendar.set(Calendar.MINUTE,0);if(dateCalendar.after(targetCalendar)) {

time= context.getString(R.string.today) +" "+time;returntime;

}else{

targetCalendar.add(Calendar.DATE,-1);if(dateCalendar.after(targetCalendar)) {

time= context.getString(R.string.yesterday) +" "+time;returntime;

}

}

time= String.format(Locale.ENGLISH,"%02d-%02d %02d:%02d", month, day, hour, minute);

CLog.d(TAG,"getTime_FormatTime_MMDDHHMM time:"+time);returntime;

}/*** 这里对方法做描述

*

*@returntime格式 nnnn-mm-dd hh:mm:ss

*@sinceV1.0*/

public synchronized static String getTime_nnnnyydd(longt) {if (t == 0) {return "";

}

Calendar cale=Calendar.getInstance();

cale.setTimeInMillis(t);int year =cale.get(Calendar.YEAR);int month = cale.get(Calendar.MONTH) + 1;int day =cale.get(Calendar.DAY_OF_MONTH);int hour =cale.get(Calendar.HOUR_OF_DAY);int minute =cale.get(Calendar.MINUTE);int second =cale.get(Calendar.SECOND);

String time= String.format(Locale.ENGLISH, "%d-%02d-%02d %02d:%02d:%02d", year, month, day, hour, minute, second);

CLog.d(TAG,"getTime_nnnnyydd time:"+time);returntime;

}/*** 这里对方法做描述

*

*@returntime格式 nnnn-mm-dd hh:mm:ss

*@sinceV1.0*/

public synchronized staticString getTime_nnnnyydd(Calendar cale) {if (cale == null) {return "";

}int year =cale.get(Calendar.YEAR);int month = cale.get(Calendar.MONTH) + 1;int day =cale.get(Calendar.DAY_OF_MONTH);int hour =cale.get(Calendar.HOUR_OF_DAY);int minute =cale.get(Calendar.MINUTE);int second =cale.get(Calendar.SECOND);

String time= String.format(Locale.ENGLISH,"%d-%02d-%02d %02d:%02d:%02d", year,month,day, hour, minute, second);

CLog.d(TAG,"getTime_nnnnyydd time:" +time);returntime;

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值