【问题标题】:Date displaying incorrect in AndroidAndroid 中日期显示不正确
【发布时间】:2012-08-07 09:03:07
【问题描述】:

我正在开发一个应用程序,当时间为 11:26 时,它显示为 11:07。我用 Calendar 实例来做。

Calendar currentDate=Calendar.getInstance();
        SimpleDateFormat ddMMyyyy=new SimpleDateFormat("dd/MM/yyyy");
        datenow=ddMMyyyy.format(currentDate.getTime());

        if(currentDate.get(Calendar.AM_PM)==Calendar.PM){
            timenow=currentDate.get(Calendar.HOUR)+":"+currentDate.get(Calendar.MONTH)+":PM";
        }else{
            timenow=currentDate.get(Calendar.HOUR)+":"+currentDate.get(Calendar.MONTH)+":AM";
        }
        new MyToast(this, "Date = "+datenow+" time = "+timenow);

输出错误怎么办?

【问题讨论】:

    标签: android date format


    【解决方案1】:

    您正在显示month 代替minute。把你的代码改成我的代码

    Calendar currentDate=Calendar.getInstance();
    SimpleDateFormat ddMMyyyy=new SimpleDateFormat("dd/MM/yyyy");
    String datenow = ddMMyyyy.format(currentDate.getTime());
    
    String timenow;
    if(currentDate.get(Calendar.AM_PM)==Calendar.PM){
        timenow=currentDate.get(Calendar.HOUR)+":"+currentDate.get(Calendar.MINUTE)+":PM";
    }else{
        timenow=currentDate.get(Calendar.HOUR)+":"+currentDate.get(Calendar.MINUTE)+":AM";
    }
    Toast.makeText(MainActivity.this, "Date = "+datenow+" time = "+timenow,Toast.LENGTH_SHORT).show();
    

    11:07 正在显示,因为 07 ==> 一年中的月份数。月份将从 0 -> 11

    计算

    【讨论】:

      【解决方案2】:

      您误看这里Calendar.HOUR + : + Calendar.MONTH。应该是这样的,

      Calendar currentDate=Calendar.getInstance();
              SimpleDateFormat ddMMyyyy=new SimpleDateFormat("dd/MM/yyyy");
              datenow=ddMMyyyy.format(currentDate.getTime());
      
              if(currentDate.get(Calendar.AM_PM)==Calendar.PM){
                  timenow=currentDate.get(Calendar.HOUR)+":"+currentDate.get(Calendar.MINUTE)+":PM";
              }else{
                  timenow=currentDate.get(Calendar.HOUR)+":"+currentDate.get(Calendar.MINUTE)+":AM";
              }
              new MyToast(this, "Date = "+datenow+" time = "+timenow);
      

      您访问的是日历类的 MONTH 对象,而不是 MINUTE

      【讨论】:

        猜你喜欢
        • 2016-04-07
        • 2015-02-14
        • 2016-06-07
        • 2018-11-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多