2013-04-30 67 views
2

我想製作一個小型報警應用程序。我想計算系統時間(當前)和時間之間的時間差從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"); 



    } 
}); 
} 

我很想有一個答案。謝謝,烏斯曼!

回答

3

試試這個

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

這裏c是,你已經創建並c2將b實例當前date timeCalender實例。


編輯

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); 

哪裏cCalendar實例與價值,在onTimeChanged

變量hour設置,minsecondsRemaining會給你剩餘的時間。

+0

它不工作。看到我更新的代碼。 – 2013-04-30 14:37:35

+0

@UsmanRiaz:plz檢查編輯的答案。 – 2013-05-01 06:45:51

1

好吧,你在Millis獲得系統時間。並用millis轉換你選擇的時間,並且只是改變它。

+0

我的系統時間在12小時格式和採摘時間是在24小時,這會不會造成我的麻煩或不? – 2013-04-30 12:47:54

+0

不,它不會,兩者都會是123456789這樣的數字。它的總時間以毫秒爲單位。 – user1304 2013-04-30 13:07:20

2

你的主要問題是當你使用一個TimePicker你只有到小時和分鐘訪問。 我想知道你想要的差異,currentSystemTime和TodayTime +小時和分鐘。

例如: 如果用戶在10:00(當前時間)選擇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 




}