2014-02-13 104 views
1
public void setCurrenttime() { 

    c = Calendar.getInstance(); 
    hour = c.get(Calendar.HOUR_OF_DAY); 
    min = c.get(Calendar.MINUTE); 
    String am_pm; 
    if (hour > 12) {    
     am_pm = "PM"; 
    } else { 
     am_pm = "AM"; 
    } 

    tvDisplayHour=(TextView)findViewById(R.id.hourtext); 
    tvDisplayHour.setText(hour+":"+ min + " "+ am_pm);  
} 

我的問題是它不會在我的模擬器中設置當前時間。設置當前時間android

該代碼有什麼問題?請幫助我是Android新手。

感謝


public void setCurrenttime() { 

    c = Calendar.getInstance(); 
    hour = c.get(Calendar.HOUR_OF_DAY); 
    min = c.get(Calendar.MINUTE); 
    String am_pm; 
    if (hour > 12) {    
     am_pm = "PM"; 
    } else { 
     am_pm = "AM"; 
    } 

    tvDisplayHour=(TextView)findViewById(R.id.hourtext); 
    tvDisplayHour.setText(hour+":"+ min + " "+ am_pm);  
} 

在這段代碼如何,我可以轉換成12小時格式?

+0

將小時和分鐘轉換爲字符串,然後通過s tring到setText –

+0

@NaveedAli他已經這樣做了... –

回答

2

您使用的代碼是從系統獲取時間而不是設置系統的時間。如果你想設置時間到系統,那麼你需要使用AlarmManager類。檢查下面的代碼,

Calendar c = Calendar.getInstance(); 
c.set(2014, 8, 15, 12, 34, 56); 
AlarmManager am = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE); 
am.setTime(c.getTimeInMillis()); 

還需要定義下列權限到您AndroidManifest.xml文件

<permission android:name="android.permission.SET_TIME" 
    android:protectionLevel="signature|system" 
    android:label="@string/permlab_setTime" 
    android:description="@string/permdesc_setTime" /> 
0

試試這個: 您將獲得當前的時間: -

符合你的要求進行修改: -

public void current_time() { 

     Calendar cal = Calendar.getInstance(); 
     int hour = cal.get(Calendar.HOUR_OF_DAY); 
     int minute = cal.get(Calendar.MINUTE); 
     int am_pm = cal.get(Calendar.AM_PM); 

     TimePickerDialog mTimePicker; 
     mTimePicker = new TimePickerDialog(InformationActivity.this, 
       new TimePickerDialog.OnTimeSetListener() { 
        @Override 
        public void onTimeSet(TimePicker timePicker, 
          int selectedHour, int selectedMinute) { 

         String minutes = ""; 

         int startTimeHour = selectedHour; 
         int startTimeMinute = selectedMinute; 

         if (selectedMinute < 10) { 
          minutes = "0" + selectedMinute; 
         } else { 
          minutes = String.valueOf(selectedMinute); 
         } 

         if (selectedHour > 12) { 
          selectedHour -= 12; 

          String timeSet_in = "PM"; 
          edt_time_in.setText(selectedHour + ":" + minutes 
            + " " + timeSet_in.toString()); 
          selectedHour += 12; 

         } else if (selectedHour == 0) { 

          selectedHour += 12; 

          timeSet_in = "AM"; 
          edt_time_in.setText(selectedHour + ":" + minutes 
            + " " + timeSet_in.toString()); 
          selectedHour -= 12; 
         } else if (selectedHour == 12) { 

          timeSet_in = "PM"; 
          edt_time_in.setText(selectedHour + ":" + minutes 
            + " " + timeSet_in.toString()); 
         } else { 

          timeSet_in = "AM"; 
          edt_time_in.setText(selectedHour + ":" + minutes 
            + " " + timeSet_in.toString()); 
         } 

         arriveLoadHour = selectedHour; 
         arriveLoadMinute = selectedMinute; 

        } 
       }, hour, minute, true);// Yes 24 hour time 
     mTimePicker.setTitle("Select Time"); 
     mTimePicker.show(); 

    } 
+0

感謝代碼,但我的問題我不會使用對話框..我只是想獲取時間,並把它放在文本視圖,並使其成爲一個12小時格式.. @ user3243147 –