activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.example.timertaskdemo.MainActivity" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /> <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /> <TextView android:id="@+id/textView3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /> <TextView android:id="@+id/textView4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /> </LinearLayout>
package com.example.timertaskdemo; import java.util.Calendar; import java.util.Date; import java.util.Timer; import java.util.TimerTask; import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.util.Log; import android.widget.TextView; public class MainActivity extends Activity { TextView textView1; TextView textView2; TextView textView3; TextView textView4; private Timer timer1; private Timer timer2; private Timer timer3; private Timer timer4; private Date time; private Handler mHandler = new Handler() { @Override public void handleMessage(Message msg) { super.handleMessage(msg); if (msg.what == 1) { textView1.setText("11111"); Log.e("TAG", "11111"); } if (msg.what == 2) { textView2.setText("22222"); Log.e("TAG", "22222"); } if (msg.what == 3) { textView3.setText("33333"); Log.e("TAG", "33333"); } if (msg.what == 4) { Calendar calendar = Calendar.getInstance(); calendar.set(Calendar.YEAR, calendar.get(Calendar.YEAR)); // 控制时 calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH)); // 控制分 calendar.set(Calendar.DATE, calendar.get(Calendar.DATE)); // 控制秒 calendar.set(Calendar.HOUR_OF_DAY, calendar.get(Calendar.HOUR_OF_DAY)); calendar.set(Calendar.MINUTE, calendar.get(Calendar.MINUTE)); calendar.set(Calendar.SECOND, calendar.get(Calendar.SECOND)); time = calendar.getTime(); textView4.setText("" + time); Log.e("TAG", "44444"); } } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textView1 = (TextView) findViewById(R.id.textView1); textView2 = (TextView) findViewById(R.id.textView2); textView3 = (TextView) findViewById(R.id.textView3); textView4 = (TextView) findViewById(R.id.textView4); timer1(); timer2(); timer3(); timer4(); } @Override protected void onDestroy() { super.onDestroy(); timer1.cancel(); timer2.cancel(); timer3.cancel(); timer4.cancel(); } /** * 2秒后开始执行,执行完毕结束 */ public void timer1() { timer1 = new Timer(); timer1.schedule(new TimerTask() { public void run() { mHandler.sendEmptyMessage(1); } }, 2000); } /** * 2秒后开始执行,执行完毕,过2秒继续循环 */ public void timer2() { timer2 = new Timer(); timer2.schedule(new TimerTask() { public void run() { mHandler.sendEmptyMessage(2); } }, 2000, 2000); } /** * 2秒后开始执行,执行完毕,过2秒继续循环 */ public void timer3() { timer3 = new Timer(); timer3.scheduleAtFixedRate(new TimerTask() { public void run() { mHandler.sendEmptyMessage(3); } }, 2000, 2000); } /** * 安排指定的任务task在指定的时间firstTime开始进行重复的固定速率period执行. */ public void timer4() { Calendar calendar = Calendar.getInstance(); calendar.set(Calendar.YEAR, calendar.get(Calendar.YEAR)); // 控制时 calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH)); // 控制分 calendar.set(Calendar.DATE, calendar.get(Calendar.DATE)); // 控制秒 calendar.set(Calendar.HOUR_OF_DAY, calendar.get(Calendar.HOUR_OF_DAY)); calendar.set(Calendar.MINUTE, calendar.get(Calendar.MINUTE)); calendar.set(Calendar.SECOND, calendar.get(Calendar.SECOND)); time = calendar.getTime(); Log.e("TAG", "" + time); timer4 = new Timer(); timer4.scheduleAtFixedRate(new TimerTask() { public void run() { mHandler.sendEmptyMessage(4); } }, time, 1000);// 这里设定将延时每天固定执行 } }
++++++++++++++++++++++++++++++++日期++++++++++++++++++++++++++++++++++++++++
DateUtil
package com.example.dateutildemo; import android.annotation.SuppressLint; import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.GregorianCalendar; import java.util.Locale; import java.util.Random; import java.util.TimeZone; @SuppressLint("SimpleDateFormat") public class DateUtil { @SuppressLint("SimpleDateFormat") public static final SimpleDateFormat simpleFormat = new SimpleDateFormat( "yyyy-MM-dd HH:mm"); @SuppressLint("SimpleDateFormat") public static final SimpleDateFormat yearFormat = new SimpleDateFormat( "yyyy-MM-dd"); public static final SimpleDateFormat simpleDateFormat = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss"); public static final TimeZone tz = TimeZone.getTimeZone("GMT+8:00"); static { simpleFormat.setTimeZone(tz); yearFormat.setTimeZone(tz); simpleDateFormat.setTimeZone(tz); } /** * 获取特定格式的时间 * * @param type * @return */ public static String getFormatDateTime(int type) { try { Calendar c = Calendar.getInstance(); c.setTimeInMillis(System.currentTimeMillis()); if (type == 1) { return yearFormat.format(c.getTime()); } if (type == 2) { return simpleFormat.format(c.getTime()); } if (type == 3) { return simpleDateFormat.format(c.getTime()); } } catch (Exception e) { e.printStackTrace(); } return ""; } }
package com.example.dateutildemo; import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.view.Menu; import android.view.MenuItem; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); String yearFormat = DateUtil.getFormatDateTime(1); String simpleFormat = DateUtil.getFormatDateTime(2); String simpleDateFormat = DateUtil.getFormatDateTime(3); //2015-12-28 13:32:36 Log.e("TAG", yearFormat); Log.e("TAG", simpleFormat); Log.e("TAG", simpleDateFormat); } }