24-hour format to 12-hour format
Asked Answered
H

7

12

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.

Heliozoan answered 17/12, 2013 at 8:29 Comment(0)
A
21

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);
    }
};
Appaloosa answered 17/12, 2013 at 9:16 Comment(0)
C
9

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"));
        }
    };
Cantonment answered 19/11, 2015 at 9:7 Comment(3)
Can you please help me to write this code in kotlin?Gaberlunzie
val isPM: Boolean = hourOfDay>= 12 tvTime.setText( java.lang.String.format( "%02d:%02d %s", if (hourOfDay=== 12 || hourOfDay=== 0) 12 else hour % 12, minute, if (isPM) "PM" else "AM" ) )Embryogeny
copying and pasting java code will automatically covert it dude.Embryogeny
T
5

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);
Toothless answered 26/2, 2017 at 20:17 Comment(0)
D
2

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

Dino answered 17/12, 2013 at 8:38 Comment(1)
yes sir.. I already did that.. i'm sorry because there is a lack information.Heliozoan
S
1

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

    }

};
Southsoutheast answered 17/12, 2013 at 9:26 Comment(0)
D
0

Why don't you use

timePicker.setIs24HourView(false);

Densitometer answered 17/12, 2013 at 8:32 Comment(1)
i try it that already but the outcome is same.. That is only a view of the time picker.Heliozoan
S
0

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

Since answered 25/9, 2018 at 11:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.