2

我在显示一个人的计算睡眠周期的正确 AM/PM 时遇到问题。例如,用户点击早上 7:00 的醒来时间,应用程序应返回晚上 11:15 的睡眠时间。在我的应用程序中,它显示的是上午 11:15 而不是下午。

你能帮我解决我到目前为止所做的事情吗:

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


        }
    };

任何帮助都非常感谢!谢谢。

4

1 回答 1

8

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

于 2012-11-05T17:59:47.393 回答