我想做一个小闹钟应用程序。我想计算系统时间(当前)和时间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");
}
});
}
我很想得到一个答案。谢谢,乌斯曼!!!