2

我想做一个小闹钟应用程序。我想计算系统时间(当前)和时间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");



    }
});
}

我很想得到一个答案。谢谢,乌斯曼!!!

4

3 回答 3

3

尝试这个

Calendar c2 = Calendar.getInstance();
c2.set(Calendar.HOUR_OF_DAY, 22);
c2.set(Calendar.MINUTE, 30);
long sub = c2.getTimeInMillis() - c.getTimeInMillis();

cCalender您创建的实例,c2将是 current 的实例date time


编辑

onClick你可以试试

Calendar cal = Calendar.getInstance();
long diff = c.getTimeInMillis() - cal.getTimeInMillis();
long diffInSec = TimeUnit.MILLISECONDS.toSeconds(diff);
int hour = (int) (diffInSec / (60 * 60));
int minremaining = (int) (diffInSec % (60 * 60));
int min = (int) (minremaining / (60));
int secondsRemaining = (int) (minremaining % (60));
Log.e("TAG", "Difference in    milliss   calendar      " + diff);
Log.e("TAG", "hour      " + hour);
Log.e("TAG", "min       " + min);
Log.e("TAG", "sec       " + secondsRemaining);

c具有值的Calendar实例在哪里,设置在onTimeChanged

变量hour,会给你剩余的时间minsecondsRemaining

于 2013-04-30T12:53:26.630 回答
2

您的主要问题是当您使用 a 时,TimePicker您只能访问Hour 和 Minute。我假设您想要区别,currentSystemTime 和 TodayTime + Hour 和 Minute。

例如:如果用户在 10:00(currentTime) 选择 20:35,你应该得到10:35。正确的?

所以这是我的解决方案(此处的完整示例:Github full example):

@Override
public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
    pickedHour = hourOfDay;
    pickedMinute = minute;
}

public void onClickListenerOfYourBUtton(View view)
{
    long currentTime = System.currentTimeMillis();
    long diffTime = 0;

    calendar = Calendar.getInstance();
    sysCurrent.setText(currentTime +"");

    calendar.setTimeInMillis(currentTime);//set to currentTime to know the current day
    calendar.set(Calendar.HOUR_OF_DAY, pickedHour); 
    calendar.set(Calendar.MINUTE, pickedMinute);

    diffTime = calendar.getTimeInMillis()- currentTime; // Here, i get the difference in hours, minutes, ...for the same day



    difference.setText(String.format("%02d:%02d",
            diffTime/hoursInMilis, 
            (diffTime%hoursInMilis)/minutesInMilis)); //Formating string




}
于 2013-04-30T14:45:02.990 回答
1

好吧,您可以在 Millis 中获得系统时间。并以毫秒为单位转换您选择的时间,然后取其不同。

于 2013-04-30T12:46:19.773 回答