2015-08-08 65 views
0

我從timepickerdialog有2個字符串格式的時間。我想不通如何比較我的第一個字符串時間到當前時間。在time1-time2期間,我想顯示一個簡單的通知。我讀過很多沒有運氣的文檔。也許一個詳細的教程或源代碼會有幫助。比較字符串時間與當前時間android

在此先感謝

這裏是我的代碼:

final Button time1 = (Button) findViewById(R.id.button1);  
    time1.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      Calendar now = Calendar.getInstance(); 
      TimePickerDialog dpd = TimePickerDialog.newInstance(
        Scheduler.this, 
        now.get(Calendar.HOUR_OF_DAY), 
        now.get(Calendar.MINUTE), true 
      ); 
      dpd.show(getFragmentManager(), "Timepickerdialog"); 
      dpd.setOnTimeSetListener(new TimePickerDialog.OnTimeSetListener() { 
       @Override 
       public void onTimeSet(RadialPickerLayout radialPickerLayout, int selectedhour, int selectedminute) { 
        time1.setText(pad(selectedhour) + " : " + pad(selectedminute));  

       } 
      }); 
     } 
    }); 


    final Button time2 = (Button) findViewById(R.id.button2);  
    time2.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      Calendar now = Calendar.getInstance(); 
      TimePickerDialog dpd = TimePickerDialog.newInstance(
        Scheduler.this, 
        now.get(Calendar.HOUR_OF_DAY), 
        now.get(Calendar.MINUTE), true 
      ); 

      dpd.show(getFragmentManager(), "Timepickerdialog"); 
      dpd.setOnTimeSetListener(new TimePickerDialog.OnTimeSetListener() { 
       @Override 
       public void onTimeSet(RadialPickerLayout radialPickerLayout, int selectedhour, int selectedminute) { 
        time2.setText(pad(selectedhour) + " : " + pad(selectedminute)); 
       } 
      }); 
     } 
    }); 

回答

1

有優秀JodaTime庫與時間相關的操作,它甚至有一個Android的味道,這裏是你一個鏈接:

https://github.com/dlew/joda-time-android

在你的情況,如果我沒有得到的東西,你有兩個時間點(小時和分鐘對 - HH:MM - 時間1一nd time2)進行比較,並確定當前時間是否在該範圍內,對嗎?

在這種情況下,我會使用JodaTime的LocalTime類,並將當前時間與time1和time2進行比較。

這可能是這樣的:

LocalTime currentTime = new LocalTime(); 
LocalTime time1 = new LocalTime(selectedhour1, selectedminute1); 
LocalTime time2 = new LocalTime(selectedhour2, selectedminute2); 

if (currenTime >= time1 && currentTime <= time2) { 
    // show your notification 
} 
+0

謝謝!!你做對了,這真的很有幫助。 – tothkris

相關問題