从字符串中将AM / PM设置为时间选择器对话框?

时间:2013-03-15 12:34:19

标签: android timepicker

我有一个TextView,其中时间以10:00 PM格式显示当用户点击该视图时,必须将同一时间设置为TimePicker对话框。我可以将HourMinutes设置为Picker,但是在AM/PM部分。我总是得到AM用户是否设置了AMPM。怎么解决?任何帮助表示赞赏。谢谢!

5 个答案:

答案 0 :(得分:3)

我遇到过类似的问题。

可能是因为您可能已将hour元素设置为“以下方式”

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

尝试将其更改为

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

希望它会帮助你。

答案 1 :(得分:0)

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

答案 2 :(得分:0)

您首先以24小时格式将数据提供给时间戳。稍后设置

public void setIs24HourView (false);

那会为你做.. ..

答案 3 :(得分:0)

试试这个,我希望我能解决你的问题

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

了解更多Time Pattern Syntax

答案 4 :(得分:0)

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

并将小时和分钟值设置为timepicker ..