【问题标题】:Using calendar to determine AM or PM dates使用日历确定上午或下午日期
【发布时间】:2011-09-08 05:59:06
【问题描述】:

使用日历类来确定上午或下午时间。

            Calendar c = Calendar.getInstance(); 
            int seconds = c.get(Calendar.SECOND);
            int minutes = c.get(Calendar.MINUTE);
            int hours = c.get(Calendar.HOUR);
            int years = c.get(Calendar.YEAR);
            int months = 1 + c.get(Calendar.MONTH);
            int days = c.get(Calendar.DAY_OF_MONTH);
            int AM_orPM = c.get(Calendar.AM_PM);

            try{
                if (hours < 12)
                {
                    String PM = "";
                    if (AM_orPM == 1)
                    {
                        PM = "PM";
                    }
                    timestamp.setText("Refreshed on " + months + "-"
                    + days + "-" + years + " " +  hours + ":" + minutes + ":" + seconds + " " + PM);
                    timestamp.setTextSize(17f);
                    timestamp.setTextColor(Color.GREEN);
                }
                else if (hours > 12)
                {
                    String AM = "";
                    if (AM_orPM == 0)
                    {
                        AM = "AM";
                    }
                    hours = hours - 12;
                    timestamp.setText("Refreshed on " + years + "-"
                    + months + "-" + days + " " +  hours + ":" + minutes + ":" + seconds + AM);
                    timestamp.setTextSize(17f);
                    timestamp.setTextColor(Color.GREEN);
                }
            }
            catch (Exception e){} 

我想根据当前时间将时间设置为上午或下午。 也出于某种原因 Calendar.MONTH 值没有给我正确的月份。差了一个,所以我必须加 1。只是想知道这是否正常?

int months = 1 + c.get(Calendar.MONTH);

【问题讨论】:

  • 附带说明,没有理由使用 if/else 子句,只需说 String AM_PM = ""; if (AM_orPM == 1){ AM_PM = "PM"; } 其他 { AM_PM = "上午"; }

标签: android class calendar


【解决方案1】:

这很正常。因为Calendar.MONTH 的索引从0 开始。所以你需要+1 来获得正确的月份。

【讨论】:

  • 这就是我的想法。我的主要问题是如何使用日历类来显示上午或下午?因为据我测试 PM 工作,但 AM 没有。
  • 是我的错误,还是你在比较小时是否小于 12? (如果是,那么就是 AM)。你反其道而行之。你可以做一个简单的if(calendar.get(Calendar.AM_PM) == 0) am_pm = "AM"; else am_pm = "PM";Calendar.AM_PM 如果是 AM,则返回 0,对于 PM,返回 1
  • 我认为它现在可以工作,但很奇怪。我不确定除了时间 > 12 之外我做错了什么;小时
【解决方案2】:

只需检查calendar.get(Calendar.AM_PM) == Calendar.AM

Calendar now = Calendar.getInstance();
if(now.get(Calendar.AM_PM) == Calendar.AM){
   // AM
}else{
   // PM
}

【讨论】:

    【解决方案3】:

    确定上午与下午是基于小时的简单计算。代码如下:

    String timeString="";
    int hour = Calendar.getInstance().get(Calendar.HOUR_OF_DAY);
    if (hour == 0) {
        timeString =  "12AM (Midnight)";  
    } else if (hour < 12) {
        timeString = hour +"AM";
    } else if (hour == 12) {
        timeString = "12PM (Noon)";
    } else {
        timeString = hour-12 +"PM";
    }
    

    【讨论】:

      【解决方案4】:

      我试过this,但它不起作用。我必须获取 AM_PM 值,然后进行比较:

      int AM_PM = c.get(Calendar.AM_PM);
      if(AM_PM == Calendar.AM){
      ...
      ...
      

      这是解决方案:

         Calendar c = Calendar.getInstance();
          int AM_PM = c.get(Calendar.AM_PM);
      
          if(AM_PM == Calendar.AM){
             //AM
          }else{
             //PM
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-03-28
        • 1970-01-01
        • 2021-12-08
        • 2020-10-27
        • 1970-01-01
        相关资源
        最近更新 更多