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>
MainActivity
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.getphoneimage;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;
import android.annotation.SuppressLint;
@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,Long date) {
try {
Calendar c = Calendar.getInstance();
c.setTimeInMillis(date);
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 "";
}
/**
* 格式化时间
* @param time 日期格式的字符串!!!!!!
* @return
*/
public static String isTodayOrYesday(String time) {
SimpleDateFormat format = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm");
if(time==null ||"".equals(time)){
return "";
}
Date date = null;
try {
//将字符串转为日期
date = format.parse(time);
} catch (ParseException e) {
e.printStackTrace();
}
Calendar current = Calendar.getInstance();
Calendar today = Calendar.getInstance(); //今天
today.set(Calendar.YEAR, current.get(Calendar.YEAR));
today.set(Calendar.MONTH, current.get(Calendar.MONTH));
today.set(Calendar.DAY_OF_MONTH,current.get(Calendar.DAY_OF_MONTH));
// Calendar.HOUR——12小时制的小时数 Calendar.HOUR_OF_DAY——24小时制的小时数
today.set( Calendar.HOUR_OF_DAY, 0);
today.set( Calendar.MINUTE, 0);
today.set(Calendar.SECOND, 0);
Calendar yesterday = Calendar.getInstance(); //昨天
yesterday.set(Calendar.YEAR, current.get(Calendar.YEAR));
yesterday.set(Calendar.MONTH, current.get(Calendar.MONTH));
yesterday.set(Calendar.DAY_OF_MONTH,current.get(Calendar.DAY_OF_MONTH)-1);
yesterday.set( Calendar.HOUR_OF_DAY, 0);
yesterday.set( Calendar.MINUTE, 0);
yesterday.set(Calendar.SECOND, 0);
current.setTimeInMillis(date.getTime());
if(current.after(today)){
return "今天 "+time.split(" ")[1];
}else if(current.before(today) && current.after(yesterday)){
return "昨天 "+time.split(" ")[1];
}else{
int index = time.indexOf("-")+1;
return time.substring(index, time.length());
}
}
/**
* 验证某个字符串是不是日期格式
* @param str
* @return
*/
public static boolean isValidDate(String str) {
boolean convertSuccess=true;
SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd HH:mm");
try {
format.setLenient(false);
format.parse(str);
} catch (ParseException e) {
e.printStackTrace();
convertSuccess=false;
}
return convertSuccess;
}
}
MainActivity
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);
}
}