Displaying the Time in AM/PM format in 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???

Answers


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

I have done like this to achieve it.

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

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

int am_pm=mycalendar.get(Calendar.AM_PM);
String amOrpm=((am_pm==Calendar.AM)?"am":"pm");

But as per my opinion simpleDateFormat would be the best use, like this:

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

It's better as the calendar by default will return 00 for 12 pm, which you might don't want.


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

I have done like this to achieve it.

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

This worked for me:

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

Need Your Help

How do I load my models inside a codeigniter hook file

codeigniter

Here's my issue. I am building an application that requires filters. I have gotten the filter system working and I can even pinpoint the actual method i want to access @ the moment. The issue is th...

Mapping UART register address to kernel for writing device drivers

c linux-kernel linux-device-driver device-driver uart

How will I map register addresses specifically UART registers to kernel for writing device drivers for UART?