2010-04-09 71 views
9

我使用的是時間選擇器,並使用下面的代碼在文本視圖中顯示的時間獲取時間AM/PM格式的時間...顯示在Android的

private TimePickerDialog.OnTimeSetListener mTimeSetListener = new TimePickerDialog.OnTimeSetListener() { 
    public void onTimeSet(TimePicker view, int hourOfDay, int minute) { 
     Toast.makeText(SendMail.this, "Your Appointment time is "+hourOfDay+":"+minute, Toast.LENGTH_SHORT).show(); 
     TextView datehid = (TextView)findViewById(R.id.timehidden); 
     datehid.setText(String.valueOf(hourOfDay)+ ":"+(String.valueOf(minute))); 
    } 
}; 

現在,問題是,當我將時間設置爲晚上8點,然後我將時間顯示爲20:0 ...我想將時間顯示爲晚上8點...我該怎麼做?

+1

很多人都已經回答了你的問題,你不迴應,因爲回答不標記。它沒有什麼動力幫助你。 – synic 2010-04-09 15:33:14

+1

另外,你的許多問題其實都有答案,但是你繼續在評論中提出更多問題,並且仍然沒有回答問題。你應該將它們標記爲已回答,然後提出一個新問題。 – synic 2010-04-09 15:34:36

+0

我仍然是Stack Overflow的新手。從現在開始,我會採取你的建議,並相應地做...謝謝你的寶貴建議... – 2010-04-15 04:01:51

回答

16

我這樣做是爲了實現它。

private TimePickerDialog.OnTimeSetListener mTimeSetListener = new TimePickerDialog.OnTimeSetListener() { 
    public void onTimeSet(TimePicker view, int hourOfDay, int minute) { 
     TextView datehid = (TextView)findViewById(R.id.timehidden); 
     if(hourOfDay>12) { 
      datehid.setText(String.valueOf(hourOfDay-12)+ ":"+(String.valueOf(minute)+"pm")); 
     } else if(hourOfDay==12) { 
      datehid.setText("12"+ ":"+(String.valueOf(minute)+"pm")); 
     } else if(hourOfDay<12) { 
      if(hourOfDay!=0) { 
       datehid.setText(String.valueOf(hourOfDay) + ":" + (String.valueOf(minute) + "am")); 
      } else { 
       datehid.setText("12" + ":" + (String.valueOf(minute) + "am")); 
      } 
     } 
    } 
};  
+0

+1,謝謝,你的想法對我有用。 – 2012-11-21 09:19:44

31
private String getReminingTime() { 
    String delegate = "hh:mm aaa"; 
    return (String) DateFormat.format(delegate,Calendar.getInstance().getTime()); 
} 
+0

爲我工作,非常簡單的方法 – 2015-03-24 12:34:38

+0

按要求工作,謝謝 – 2015-12-14 12:43:08

+0

它也給'Am/PM'以及?似乎不會 – eRaisedToX 2017-04-18 06:30:20

2

我這樣做是爲了實現它。

TimePickerDialog.OnTimeSetListener t = new TimePickerDialog.OnTimeSetListener() { 
    public void onTimeSet(TimePicker view, int hourOfDay, int minute) { 
     myCalendar.set(Calendar.HOUR_OF_DAY, hourOfDay); 
     myCalendar.set(Calendar.MINUTE, minute); 
     update_From_Time(); 
    } 
}; 

DateFormat frmTime=DateFormat.getTimeInstance(); 
protected void update_From_Time() { 
    s = frmTime.format(myCalendar.getTime()); 
    btnFromTime.setText(s); 
} 
+0

這是一個完美的... – 2013-01-31 07:25:31

0

這爲我工作:

public void onTimeSet(TimePicker view, int hourOfDay, int minute) { 
    String am_pm = ""; 

    Calendar datetime = Calendar.getInstance(); 
    datetime.set(Calendar.HOUR_OF_DAY, hourOfDay); 
    datetime.set(Calendar.MINUTE, minute); 

    if (datetime.get(Calendar.AM_PM) == Calendar.AM) 
     am_pm = "AM"; 
    else if (datetime.get(Calendar.AM_PM) == Calendar.PM) 
     am_pm = "PM"; 

    String strHrsToShow = (datetime.get(Calendar.HOUR) == 0) ?"12":datetime.get(Calendar.HOUR)+""; 

    ((Button)getActivity().findViewById(R.id.btnEventStartTime)).setText(strHrsToShow+":"+datetime.get(Calendar.MINUTE)+" "+am_pm); 
} 
1
int am_pm=mycalendar.get(Calendar.AM_PM); 
String amOrpm=((am_pm==Calendar.AM)?"am":"pm"); 

但是按我的看法SimpleDateFormat的是物盡其用,是這樣的:

final Calendar c = Calendar.getInstance(TimeZone.getTimeZone("America/Chicago")); 
SimpleDateFormat format = new SimpleDateFormat("h:mm a"); 
format.setTimeZone(c.getTimeZone()); 
String myFormatted time = format.format(c.getTime()); 

這是由日曆更好默認情況下會在12點後返回00,您可能不想要。

5
SimpleDateFormat df = new SimpleDateFormat("HH:mm:ss a"); 
String currentTime = df.format(currnetDateTime.getTime()); 
+1

對於任何人可能會猜測在Pravin的代碼中'currnetDateTime'就像我一樣;),它是'Calendar currnetDateTime = Calendar.getInstance();'然後'SimpleDateFormat df =新的SimpleDateFormat(「HH:mm:ss a」); String currentTime = df.format(currnetDateTime.getTime());' – eRaisedToX 2017-04-18 06:44:51

1
Calendar now = Calendar.getInstance(); 
if(now.get(Calendar.AM_PM) == Calendar.AM){ 
    // AM 
    System.out.println(""+now.get(Calendar.HOUR)+":AM"); 
}else{ 
    // PM 
    System.out.println(""+now.get(Calendar.HOUR)+":PM"); 
}