Set AM / PM in the dialog for selecting time from the line?

I have a TextView in which the time is displayed in the format 10:00 PMWhen the user clicks this view, a dialog should be set at the same time TimePicker. Can I install Hourand Minutesto Picker, but in the section AM/PM. I always get AMwhether user is installed AMor PM. How can this be solved? any help is appreciated. Thank you

+2
source share
5 answers

I had this type of similar problem.

Perhaps this could be because you could set your element houras "below"

hour = String.valueOf(cal.get(Calendar.HOUR));

Try changing it to

hour = String.valueOf(cal.get(Calendar.HOUR_OF_DAY));

Hope this helps you.

+3
private Calendar calendar_date=null;
private TimePicker timePicker;

calendar_date = Calendar.getInstance();
timePicker= (TimePicker)dialog.findViewById(R.id.timePicker1);
timePicker.setOnTimeChangedListener(new OnTimeChangedListener() {
        @Override
        public void onTimeChanged(TimePicker picker, int hour, int minute) {
            calendar_date.set(datePicker.getYear(), datePicker.getMonth(), datePicker.getDayOfMonth(), hour, minute);
            Log.i("TIME", getHourIn12Format(hour)+":"+minute+" "+getAMPM(calendar_date));
        }
    });

private int getHourIn12Format(int hour24) {
    int hourIn12Format = 0;
    if(hour24==0)
        hourIn12Format = 12;
    else if(hour24<=12)
        hourIn12Format = hour24;
    else
        hourIn12Format = hour24-12;
    return hourIn12Format;
}

private String getAMPM(Calendar calendar) {
    return (calendar.get(Calendar.AM_PM)==(Calendar.AM))? "AM":"PM";
}
0

timepicker 24- .

public void setIs24HourView (false);

..

0
source

Try this, I hope that solves your problem.

Date date = Calendar.getInstance().getTime();
SimpleDateFormat formatter = new SimpleDateFormat("MMM,dd,yyyy hh:mm a");
String mDay_time = formatter.format(date);

For More Time Template Syntax

0
source
         //String selectedtime ="03:15 PM"

         //convert time 12 hrs to 24 hrs
        SimpleDateFormat displayFormat = new SimpleDateFormat("HH:mm");
           SimpleDateFormat parseFormat = new SimpleDateFormat("hh:mm a");
           Date date = null;
        try {
            date = parseFormat.parse(selectedtime);
              System.out.println(parseFormat.format(date) + " = " + displayFormat.format(date));
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        String twentyfourhrstime=displayFormat.format(date);



         //split hrs and min
        String[] parts111 = twentyfourhrstime.split(":");
        String part11 = parts111[0]; // 004
        String part22 = parts111[1];

        System.out.println("value11>>>>"+part11);
        System.out.println("value22>>>>"+part22);
        hour = Integer.parseInt(part11);
        min =Integer.parseInt(part22);

and set the hour and min for timepicker ..

0
source

Source: https://habr.com/ru/post/1661411/


All Articles