//主页面
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.exddample.time.MainActivity" >
<TextView
android:id="@+id/tv_time1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<TextView
android:id="@+id/tv_time2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/tv_time1"
android:text="@string/hello_world" />
<TextView
android:id="@+id/tv_time3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/tv_time2"
android:text="@string/hello_world" />
<Button
android:id="@+id/btn_jump"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="点击跳转倒计时" />
</RelativeLayout>
///
//倒计时页面
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<TextView
android:id="@+id/tv_timeshow"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="倒计时"
android:textSize="30sp" />
</LinearLayout>
//主页面
package com.exddample.time;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
@SuppressLint({ "SimpleDateFormat", "UseValueOf" })
public class MainActivity extends Activity {
private TextView time1;
private TextView time2;
private TextView time3;
private Button btn_jump;
private long diff;
private int days;
private int hours;
private int minutes;
private int seconds;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
time1 = (TextView) findViewById(R.id.tv_time1);
time2 = (TextView) findViewById(R.id.tv_time2);
time3 = (TextView) findViewById(R.id.tv_time3);
btn_jump = (Button) findViewById(R.id.btn_jump);
// // String time="1463126553000";
// 获取当前时间
Date currentTime = new Date();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String date1 = formatter.format(currentTime);
time1.setText(date1);
// 获取服务器返回的时间戳 转换成"yyyy-MM-dd HH:mm:ss"
String date2 = formatData("yyyy-MM-dd HH:mm:ss", 1463126553);
time2.setText(date2);
// 计算的时间差
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
Date d1 = df.parse(date1);
Date d2 = df.parse(date2);
diff = d1.getTime() - d2.getTime();
days = (int) (diff / (1000 * 60 * 60 * 24));
hours = (int) ((diff - days * (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
minutes = (int) ((diff - days * (1000 * 60 * 60 * 24) - hours
* (1000 * 60 * 60))
/ (1000 * 60));
seconds = (int) ((diff - days * (1000 * 60 * 60 * 24) - hours
* (1000 * 60 * 60) - minutes * (1000 * 60)) / 1000);
time3.setText("" + days + "天" + hours + "小时" + minutes + "分"
+ seconds + "秒");
} catch (Exception e) {
e.printStackTrace();
}
btn_jump.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent();
intent.putExtra("hours", hours);
intent.putExtra("minutes", minutes);
intent.putExtra("seconds", seconds);
intent.setClass(getApplicationContext(), TimeActivity.class);
startActivity(intent);
}
});
}
public static String formatData(String dataFormat, long timeStamp) {
if (timeStamp == 0) {
return "";
}
timeStamp = timeStamp * 1000;
String result = "";
SimpleDateFormat format = new SimpleDateFormat(dataFormat);
result = format.format(new Date(timeStamp));
return result;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
//倒计时页面
package com.exddample.time;
import java.util.Timer;
import java.util.TimerTask;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.TextView;
@SuppressLint("HandlerLeak")
public class TimeActivity extends Activity {
private TextView tv_timeshow;
private TimerTask timerTask;
Timer timer;
final static String tag = "tag";
private int hour;
private int minute;
private int second;
public TimeActivity() {
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_time);
Intent intent = getIntent();
hour = intent.getIntExtra("hours", -1);
minute = intent.getIntExtra("minutes", -1);
second = intent.getIntExtra("seconds", -1);
tv_timeshow = (TextView) findViewById(R.id.tv_timeshow);
tv_timeshow.setText(hour + ":" + minute + ":" + second);
timerTask = new TimerTask() {
@Override
public void run() {
Message msg = new Message();
msg.what = 0;
handler.sendMessage(msg);
}
};
timer = new Timer();
timer.schedule(timerTask, 0, 1000);
}
Handler handler = new Handler() {
public void handleMessage(Message msg) {
System.out.println("handle!");
if (hour == 0) {
if (minute == 0) {
if (second == 0) {// 时间到
tv_timeshow.setText("Time out !");
if (timer != null) {
timer.cancel();
timer = null;
}
if (timerTask != null) {
timerTask = null;
}
} else {// 00:30,00:08
second--;
if (second >= 10) {
tv_timeshow.setText("0" + hour + ":" + "0" + minute
+ ":" + second);
} else {
tv_timeshow.setText("0" + hour + ":" + "0" + minute
+ ":0" + second);
}
}
} else {// minute!=0,33:00->32:59
if (second == 0) {
second = 59;
minute--;
if (minute >= 10) {// 33:30
tv_timeshow.setText("0" + hour + ":" + minute + ":"
+ second);
} else {// 06:30
tv_timeshow.setText("0" + hour + ":" + "0" + minute
+ ":" + second);
}
} else {// 32:30->32:29
second--;
if (second >= 10) {
if (minute >= 10) {
tv_timeshow.setText("0" + hour + ":" + minute
+ ":" + second);
} else {// 09:30
tv_timeshow.setText("0" + hour + ":" + "0"
+ minute + ":" + second);
}
} else {// 12:30
if (minute >= 10) {
tv_timeshow.setText("0" + hour + ":" + minute
+ ":0" + second);
} else {// 08:30
tv_timeshow.setText("0" + hour + ":" + "0"
+ minute + ":0" + second);
}
}
}
}
}
// hour>0
else {
if (minute == 0) {// hour>0,minute=0
if (second == 0) {
minute = 59;
second = 59;
hour--;
if (hour >= 10) {
if (second >= 10){
tv_timeshow.setText(hour + ":" + minute + ":" + second);
}else{
tv_timeshow.setText(hour + ":" + minute + ":0" + second);
}
} else {
if (second >= 10){
tv_timeshow.setText("0"+hour + ":" + minute + ":" + second);
}else{
tv_timeshow.setText("0"+hour + ":" + minute + ":0" + second);
}
}
} else {
second--;
if (hour >= 10) {
if (second >= 10){
tv_timeshow.setText(hour + ":0" + minute + ":" + second);
}else{
tv_timeshow.setText(hour + ":0" + minute + ":0" + second);
}
} else {
if (second >= 10){
tv_timeshow.setText("0"+hour + ":0" + minute + ":" + second);
}else{
tv_timeshow.setText("0"+hour + ":0" + minute + ":0" + second);
}
}
}
} else {// hour>0,minute!=0
if (second == 0) {
second = 59;
minute--;
if (hour >= 10) {
if (second >= 10) {
if (minute >= 10) {
tv_timeshow.setText(hour + ":" + minute + ":"
+ second);
} else {
tv_timeshow.setText(hour + ":0" + minute + ":"
+ second);
}
} else {
if (minute >= 10) {
tv_timeshow.setText(hour + ":" + minute + ":0"
+ second);
} else {
tv_timeshow.setText(hour + ":0" + minute
+ ":0" + second);
}
}
} else {
if (second >= 10) {
if (minute >= 10) {
tv_timeshow.setText("0" + hour + ":" + minute
+ ":" + second);
} else {
tv_timeshow.setText("0" + hour + ":0" + minute
+ ":" + second);
}
} else {
if (minute >= 10) {
tv_timeshow.setText("0" + hour + ":" + minute
+ ":0" + second);
} else {
tv_timeshow.setText("0" + hour + ":0" + minute
+ ":0" + second);
}
}
}
} else {
second--;
if (hour >= 10) {
if (second >= 10) {
if (minute >= 10) {
tv_timeshow.setText(hour + ":" + minute + ":"
+ second);
} else {
tv_timeshow.setText(hour + ":0" + minute + ":"
+ second);
}
} else {
if (minute >= 10) {
tv_timeshow.setText(hour + ":" + minute + ":0"
+ second);
} else {
tv_timeshow.setText(hour + ":0" + minute
+ ":0" + second);
}
}
} else {
if (second >= 10) {
if (minute >= 10) {
tv_timeshow.setText("0" + hour + ":" + minute
+ ":" + second);
} else {
tv_timeshow.setText("0" + hour + ":0" + minute
+ ":" + second);
}
} else {
if (minute >= 10) {
tv_timeshow.setText("0" + hour + ":" + minute
+ ":0" + second);
} else {
tv_timeshow.setText("0" + hour + ":0" + minute
+ ":0" + second);
}
}
}
}
}
}
};
};
}