Timepicker错误返回AM / PM android

时间:2012-11-05 17:26:17

标签: android

我在显示计算出的人的睡眠周期的正确AM / PM时出现问题。例如,用户在早上7点点击他的清醒时间,应用应该在晚上11:15返回休眠时间。在我的应用程序中,它显示的是上午11:15而不是PM。

你能帮助我到目前为止所做的事情:

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

        public void onTimeSet( TimePicker view, int selectedHour,
                int selectedMinute ) {
            // Gets current time
            Calendar c = Calendar.getInstance();

            hour = selectedHour;
            minute = selectedMinute;

            // set waking time into textview
            StringBuilder sb = new StringBuilder();
            if(hour>=12){                      
              sb.append(hour-12).append( ":" ).append(minute).append(" PM");
            }else{
              sb.append(hour).append( ":" ).append(minute).append(" AM");
            }
            wakingtime.setText(sb);

            // Assign hour set in the picker
            c.set( Calendar.HOUR, selectedHour );
            c.set( Calendar.MINUTE, selectedMinute );

            // Have Calendar calculate the substraction of hours and minutes
            c.add( Calendar.HOUR, SUB_HOUR );
            c.add( Calendar.MINUTE, SUB_MINUTE );

            // Get the hour and the minute calculated
            hour = c.get( Calendar.HOUR );
            minute = c.get( Calendar.MINUTE );

            StringBuilder sb2 = new StringBuilder();
            if(hour>=12){                      
              sb2.append(hour-12).append( ":" ).append(minute).append(" PM");
            }else{
              sb2.append(hour).append( ":" ).append(minute).append(" AM");
            }
            answer.setText(sb2);

            // set current time into textview
            //answer.setText( "You must go to bed at " + new StringBuilder().append(pad(hour))
                //  .append(":").append(pad(minute)) );


        }
    };

真的很感激任何帮助!感谢。

1 个答案:

答案 0 :(得分:8)

请参阅评论中与@Simon的小讨论,您要设置的日历变量是Calendar.HOUR_OF_DAY。 Here是你正在设定和获得的。你想要那个低于那个的那个。