Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Displaying the Time in AM/PM format in android

Tags:

android

I am getting the time using a time picker and displaying the time in the text view using following code...

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

Now, the issue is when i set the time as 8:00 pm then i get the time displayed as 20:0... I want to display the time as 8:00 pm... How can i do that???

like image 952
Rahul Kalidindi Avatar asked Apr 09 '10 06:04

Rahul Kalidindi


People also ask

How do you display time in AM and PM?

Pattern “hh:mm aa” and “HH:mm aa”, here HH is used for 24 hour format without AM/PM and the hh is used for 12 hour format with AM/PM. aa – AM/PM marker. In this example we are displaying current date and time with AM/PM marker.

How do you convert HH mm to HH mm SS?

To convert hh:mm:ss time format to minutes: =((HOUR(A2)*60)+MINUTE(A2)+(SECOND(A2)/60)); To convert hh:mm:ss time format to seconds: =HOUR(A2)*3600 + MINUTE(A2)*60 + SECOND(A2).


2 Answers

private String getReminingTime() {
    String delegate = "hh:mm aaa";
    return (String) DateFormat.format(delegate,Calendar.getInstance().getTime());
}
like image 88
arlen Avatar answered Oct 04 '22 08:10

arlen


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");
}
like image 25
Linh Avatar answered Oct 04 '22 09:10

Linh