Android日期/时间显示0而不是12

时间:2012-12-15 10:33:44

标签: android date time

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

 // 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。谢谢!

2 个答案:

答案 0 :(得分:2)

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

您已经设置了hour后再次设置。

答案 1 :(得分:-1)

使用此选项检查上午或下午。

c.get(Calendar.AM_PM);

请参阅此处了解文档Calendar.AM_PM