我想製作一個小型報警應用程序。我想計算系統時間(當前)和時間之間的時間差從TimePicker
(由用戶選擇)。我找不到一些好的解決方案。這裏是我的代碼如何獲取timePicker與系統時間的時差
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_alarm_list);
timePicker = (TimePicker) findViewById(R.id.timePicker1);
timePicker.setIs24HourView(true);
b = (Button) findViewById(R.id.button1);
c = Calendar.getInstance();
timePicker.setOnTimeChangedListener(new OnTimeChangedListener() {
@Override
public void onTimeChanged(TimePicker arg0, int hours, int minutes) {
c.set(Calendar.HOUR_OF_DAY, hours);
c.set(Calendar.MINUTE, minutes);
chosenTimeMills = c.getTimeInMillis();
}
});
b.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Date currentDate = new Date(System.currentTimeMillis());
c.set(Calendar.HOUR_OF_DAY,currentDate.getHours());
c.set(Calendar.MINUTE, currentDate.getMinutes());
systemTimeMills = c.getTimeInMillis();
long difference =systemTimeMills-chosenTimeMills;
Date date = new Date(difference);
Log.d("TAGG", "Difference is: "+date.getHours()+" Hours and "+date.getMinutes()+" minutes");
}
});
}
我很想有一個答案。謝謝,烏斯曼!
它不工作。看到我更新的代碼。 – 2013-04-30 14:37:35
@UsmanRiaz:plz檢查編輯的答案。 – 2013-05-01 06:45:51