24-hour format to 12-hour format

22.6k views Asked by At

I have problem with converting 24-hour format to 12 hour format. This is my code.

    private TimePickerDialog.OnTimeSetListener mTimeSetListener =
        new TimePickerDialog.OnTimeSetListener() {

            @Override
            public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                // TODO Auto-generated method stub
                ViewGroup vg=(ViewGroup) view.getChildAt(0);
                hour = hourOfDay;
                minutes = minute;
                String am_pm = (hourOfDay < 12) ? "AM" : "PM";
                SimpleDateFormat sdf = new SimpleDateFormat("hh:mm a",Locale.US);
                String tme = hour +":"+ minutes+am_pm;
                et1.setText(tme);
            }
        };

I shift to timepicker dialog from alert dialog because i don't know how to change 24-hour to 12-hour format

et1.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                new TimePickerDialog(MainActivity.this, mTimeSetListener, hour, minutes, false).show();
            }
        });

    }  

This is the proof that i already turn the boolean impression to false.

7

There are 7 answers

0
Kanwaljit Singh On BEST ANSWER

Try this-

private TimePickerDialog.OnTimeSetListener mTimeSetListener =
new TimePickerDialog.OnTimeSetListener() {

    @Override
    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
        // TODO Auto-generated method stub
        hour = hourOfDay;
        minutes = minute;
        String timeSet = "";
        if (hour > 12) {
          hour -= 12;
          timeSet = "PM";
        } else if (hour == 0) {
          hour += 12;
          timeSet = "AM";
        } else if (hour == 12){
          timeSet = "PM";
        }else{
          timeSet = "AM";
        }

        String min = "";
        if (minutes < 10)
         min = "0" + minutes ;
        else
         min = String.valueOf(minutes);

        // Append in a StringBuilder
        String aTime = new StringBuilder().append(hour).append(':')
        .append(min ).append(" ").append(timeSet).toString();
        et1.setText(aTime);
    }
};
0
Amritha On

Pls try with this code

  int Hour=0;
int Minute=0;
int new_hour=0;
 String new_minute=null;
TimePickerDialog.OnTimeSetListener time = new TimePickerDialog.OnTimeSetListener() {



public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
         Hour = hourOfDay;
         Minute = minute;

        String time_form = hourOfDay + ":" + minute + ":00";
        int r = minute / 10;
        if (r == 0) {
            new_minute = "0" + minute;
        } else {
            new_minute = minute + "";
        }
        if (Hour > 12) {
            new_hour = (hourOfDay - 12);
            edit_text.setText(new StringBuilder().append(new_hour)
                    .append(":").append(new_minute)
                    + " PM");
        } else if (Hour == 12) {

            new_hour = hourOfDay;
            edit_text.setText(new StringBuilder().append(new_hour)
                    .append(":").append(new_minute)
                    + " PM");
        }

        else {
            new_hour = hourOfDay;
            edit_text.setText(new StringBuilder().append(new_hour)
                    .append(":").append(new_minute)
                    + " AM");
        }

    }

};
1
Madhur Ahuja On

Why don't you use

timePicker.setIs24HourView(false);

1
Serhii Bohutskyi On

There is a constructor parameter

public TimePickerDialog (Context context, TimePickerDialog.OnTimeSetListener callBack, int hourOfDay, int minute, boolean is24HourView)

see docs. And for formatting use android.text.format.DateFormat

0
Asad On

this will solve your inital 0 problem as well

String time = "", md = " AM";
if(selectedHour == 0) selectedHour = 12;
else if(selectedHour > 12) {selectedHour -= 12; md = " PM";}
else if(selectedHour == 12) md = " PM";

if(selectedHour < 10) time = "0";
time += selectedHour;
time += ":";
if(selectedMinute < 10) time += "0";
time += selectedMinute;
time += md;
et_time.setText(time);

output e.g: 02:05 AM instead of 2:5

3
Azim Ansari On

I know I am too late but just two lines make it perfect.

private TimePickerDialog.OnTimeSetListener timeSetListener = new TimePickerDialog.OnTimeSetListener() {
        @Override
        public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
            boolean isPM = (hourOfDay >= 12);
            tvTime.setText(String.format("%02d:%02d %s", (hourOfDay == 12 || hourOfDay == 0) ? 12 : hourOfDay % 12, minute, isPM ? "PM" : "AM"));
        }
    };
0
Alex Andres On

This is the way I did it. Formats SimpleDateFormat takes https://developer.android.com/reference/java/text/SimpleDateFormat.html#format

TimePickerDialog timePickerDialog=  new TimePickerDialog(NotificationActivity.this,
    new TimePickerDialog.OnTimeSetListener() {
        @Override
        public void onTimeSet(TimePicker timePicker, int hour, int minute) {

            Time time = new Time(hour, minute, 0);

            //little h uses 12 hour format and big H uses 24 hour format
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("h:mma");

            //format takes in a Date, and Time is a sublcass of Date
            String s = simpleDateFormat.format(time);
            toTime.setText(s);
        }
    }, hour, minute, false);