简体   繁体   中英

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???

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 );
}
// Best Result Give Bapu_Darbar:::

Date dt = new Date(); int hours = dt.getHours(); int min = dt.getMinutes(); int am_pm = Calendar.AM_PM;

    if(hours>=1 || hours<=3 && am_pm == Calendar.AM){
        spl_text.setText("Welcome \n Good Night,Sweet Dream");
    }else if(hours>=4 || hours<=12 && am_pm == Calendar.AM){
        spl_text.setText("Welcome \n Good Morning,Have A Nice Day");
    }else if(hours>=13 || hours<=17 && am_pm == Calendar.PM){
        spl_text.setText("Welcome \n Good Afternoon");
    }else if(hours>=18 || hours<=20 && am_pm == Calendar.PM){
        spl_text.setText("Welcome \n Good Evening");
    }else if(hours>=21 || hours<=24 && am_pm == Calendar.PM){
        spl_text.setText("Welcome \n Good Night,Sweet Dream");
    }

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM