0

我想知道我下面的代码有什么问题:

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

        // For alternative times
        c.add( Calendar.HOUR, SUB_SIX_HOUR );
        c.add( Calendar.MINUTE, SUB_FLAT_MINUTE );

        hour = c.get( Calendar.HOUR );
        minute = c.get( Calendar.MINUTE );
        hour = c.get( Calendar.HOUR_OF_DAY );

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


        alternative.setText( "Alternative times: " + sb3 + " (9 hours)" + sb4 + " (6 hours)" );

请帮我弄清楚当计算的时间是午夜 12:00 时如何显示 12 而不是 0。谢谢!

4

2 回答 2

2
 hour = c.get( Calendar.HOUR );
 minute = c.get( Calendar.MINUTE );
 hour = c.get( Calendar.HOUR_OF_DAY );

您在设置hour后再次设置。

于 2012-12-15T10:48:39.693 回答
-1

使用它来检查上午或下午。

c.get(Calendar.AM_PM);

请参阅此处获取文档Calendar.AM_PM

于 2012-12-15T11:01:21.163 回答