开发者

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);开发者_如何学Python
        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???


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


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


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.


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


The solution can be setting alert dialog of two char sequence "AM" and "PM". below is an example.

@Override
    public void onTimeSet(TimePickerDialog view, int hourOfDay, int minute, int second) {

        CharSequence [] sequences = {
                "AM",
                "PM"
        };
        AlertDialog.Builder alert = new AlertDialog.Builder(getActivity(),AlertDialog.THEME_DEVICE_DEFAULT_DARK);
        alert.setItems(sequences, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                if (which ==0){
                    if (minute ==0){
                        time.setText(hourOfDay+":"+minute+"0"+"AM");
                    }else {
                        time.setText(hourOfDay + ":" + minute+"AM");
                    }
                }else{
                    if (minute ==0){
                        time.setText(hourOfDay+":"+minute+"0"+"PM");
                    }else {
                        time.setText(hourOfDay + ":" + minute+"PM");
                    }
                }
            }
        });
        alert.show();
    }

Hope this help anyone!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜