我正在尝试将此倒计时倒数到以天/小时/分钟/秒为单位的事件当天。我需要更改此代码,以便在启动时使用当前的Android手机时间和日期,根据手机显示的时间和日期显示剩余时间。目前开始时间和日期是由我设置的,每次我在模拟器上安装和运行应用程序时都会显示34天,所以如果我明天安装并运行它就不会显示33天但是34天了......我找不到任何问题有一个有用的答案,我在过去几天看了好几个地方。这是我的事件活动java,我在相对布局中使用TextView。
extends Activity {
Button button01;
CountDownTimer mCountDownTimer;
TextView mTextView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.event_activity);
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy, hh:mm");
Date a = null, b = null;
try {
a = sdf.parse("24-02-2014, 14:30");
b = sdf.parse("06-01-2014, 02:04");
} catch (ParseException e) {
e.printStackTrace();
}
mTextView = (TextView) findViewById(R.id.timer);
mCountDownTimer = new CountDownTimer(a.getTime() - b.getTime(), 1000) {
StringBuilder time = new StringBuilder();
@Override
public void onFinish() {
mTextView.setText(DateUtils.formatElapsedTime(0));
//mTextView.setText("Times Up!");
}
@Override
public void onTick(long millisUntilFinished) {
time.setLength(0);
// Use days if appropriate
if(millisUntilFinished > DateUtils.DAY_IN_MILLIS) {
long count = millisUntilFinished / DateUtils.DAY_IN_MILLIS;
if(count > 1)
time.append(count).append(" days, ");
else
time.append(count).append(" day, ");
millisUntilFinished %= DateUtils.DAY_IN_MILLIS;
}
if(millisUntilFinished > DateUtils.HOUR_IN_MILLIS) {
long count2 = millisUntilFinished / DateUtils.HOUR_IN_MILLIS;
if(count2 > 1)
time.append(count2).append(" hours, ");
else
time.append(count2).append(" hour, ");
millisUntilFinished %= DateUtils.HOUR_IN_MILLIS;
}
if(millisUntilFinished > DateUtils.MINUTE_IN_MILLIS) {
long count3 = millisUntilFinished / DateUtils.MINUTE_IN_MILLIS;
if(count3 > 1)
time.append(count3).append(" minutes, ");
else
time.append(count3).append(" minute, ");
millisUntilFinished %= DateUtils.MINUTE_IN_MILLIS;
}
time.append(DateUtils.formatElapsedTime(Math.round(millisUntilFinished / 1000d)));
mTextView.setText(time.toString());
}
}.start();
}
请帮助,如何更新当前时间,而不是自己添加开始日期和时间!当我运行模拟器时,它没有显示正确的开始时间,因为它运行它所花费的时间。感谢
-KG
答案 0 :(得分:0)
粘贴此代码
import android.os.Bundle;
import android.app.Activity;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import android.widget.TextView;
public class MainActivityCalDate extends Activity {
@SuppressLint("SimpleDateFormat")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.cal_date);
TextView tv1;
tv1 = (TextView) findViewById(R.id.textView1);
SimpleDateFormat dfDate = new SimpleDateFormat("dd/MM/yyyy");
java.util.Date d = null;
java.util.Date d1 = null;
Calendar cal = Calendar.getInstance();
try {
d = dfDate.parse("13/02/2014 ");
d1 = dfDate.parse(dfDate.format(cal.getTime()));//Returns 15/10/2012
} catch (java.text.ParseException e) {
e.printStackTrace();
}
int diffInDays = (int) ((d.getTime() - d1.getTime())/ (1000 * 60 * 60 * 24));
System.out.println(diffInDays);
tv1.setText(""+diffInDays);
}
}
它会起作用。您可以从模拟器日期和时间设置更改日期,然后重新启动活动,它将显示当天的更新结果。如果您有任何疑问,请问我。