【问题标题】:How to convert concatenated time and date string into date?如何将连接的时间和日期字符串转换为日期?
【发布时间】:2016-08-15 17:29:33
【问题描述】:

我想为此设置一个通知,我想获取日期和时间。在我的数据库中,我以不同的方式保存了警报时间和警报日期。

警报日期格式为:

SimpleDateFormat  df = new SimpleDateFormat("d MMM yyyy");

警报时间格式为:

SimpleDateFormat  df = new SimpleDateFormat("hh:mm a");

现在我尝试连接警报时间字符串和警报日期字符串并将其格式化为日期对象,但它没有得到格式。

试图转换成日期:

public void setAlertTime() {
    try {
        Date date = new Date();

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("E MMM dd HH:mm:ss zzzz yyyy");

        mAlertDateTime = mAlertDate + mAlertTime;

        date = simpleDateFormat.parse(mAlertDateTime);

        Log.d("AlertDate",String.valueOf(date));
        Toast.makeText(AddTaskActivity.this,String.valueOf(date),Toast.LENGTH_SHORT).show();


    } catch (ParseException ex) {}
}

我没有得到日期值。尝试在 toast 中打印日志中的日期值,但它没有显示任何内容。怎么了?

【问题讨论】:

  • 请发布您尝试在变量mAlertDatemAlertTime 中传递的示例数据
  • 警报日期 2016 年 4 月 22 日警报时间 08:00 这是来自日志..@JordiCastilla

标签: android date simpledateformat


【解决方案1】:

尝试在日期和时间之间添加空格

public void setAlertTime() {
    try {
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("E MMM dd HH:mm:ss zzzz yyyy");
        mAlertDateTime = mAlertDate + " " + mAlertTime;
        date = simpleDateFormat.parse(mAlertDateTime);
        Log.d("AlertDate",String.valueOf(date));
        Toast.makeText(AddTaskActivity.this,String.valueOf(date),Toast.LENGTH_SHORT).show();
    } catch (ParseException ex) {}
}

【讨论】:

    【解决方案2】:

    你需要改变两件事:

    • 连接日期时加空格
    • 更改您的SimpleDateFormat(也将DateFormats 与空格连接起来):

    String mAlertDate = "10 Mar 2016";
    String mAlertTime = "8:00 PM";
    
    String mAlertDateTime = mAlertDate + " " + mAlertTime;
    //                                    ↑ space!!!
    
    // date format concatenating other date formats
    SimpleDateFormat  dft = new SimpleDateFormat("d MMM yyyy HH:mm a");
    Date d = dft.parse(mAlertDateTime);
    
    System.out.println(dft.format(d));
    

    输出:

    10 mar 2016 08:00 AM
    

    【讨论】:

      【解决方案3】:

      如果你合并格式,那么你的格式是

       SimpleDateFormat timeStampFormat = new SimpleDateFormat("d MMM yyyy HH:mm a");
      

      现在添加带有空格的日期和时间,因此它是这样的格式形式

       mAlertDateTime = mAlertDate + " " +  mAlertTime;
      

      现在将其解析为格式。

      date = timeStampFormat.parse(mAlertDateTime);
      

      定义你的转换格式

       SimpleDateFormat simpleDateFormat = new SimpleDateFormat("E MMM dd HH:mm:ss zzzz yyyy");
      

      最后解析它。

       date2 = simpleDateFormat.parse(simpleDateFormat.format(date));
      

      注意:我正在解释的上层内容。

      现在就这样做:

      定义这个全局:

      String mAlertDate = "4 May 2016";
      String  mAlertTime = "01:30 PM";
      
      Date date,date2;
      

      并在 try catch 中使用编写此代码,

      try {
      
       SimpleDateFormat timeStampFormat = new SimpleDateFormat("d MMM yyyy HH:mm a");
      
                   mAlertDateTime = mAlertDate + " " +  mAlertTime;
                   date = timeStampFormat.parse(mAlertDateTime);
      
                  SimpleDateFormat simpleDateFormat = new SimpleDateFormat("E MMM dd HH:mm:ss zzzz yyyy");
                  date2 = simpleDateFormat.parse(simpleDateFormat.format(date));
      
      Toast.makeText(AddTaskActivity.this, date2.toString() ,Toast.LENGTH_SHORT).show();
      
      
       } catch (ParseException ex) {}
      }
      

      【讨论】:

        【解决方案4】:
        1. 更改 SimpleDateFormat 参数以适应输入。
        2. 在“mAlertDate”和“mAlertTime”之间添加一个空格。
        3. 别忘了设置语言环境。

          String mAlertDate = "10 Mar 2016";
          String mAlertTime = "08:00";
          
          Date date = new Date();
          
          SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd MMM yyyy HH:mm", Locale.ENGLISH);
          String mAlertDateTime = mAlertDate + " " + mAlertTime;
          try {
              date = simpleDateFormat.parse(mAlertDateTime);
          } catch (ParseException ex) {
              Logger.getLogger(LectorPDF.class.getName()).log(Level.SEVERE, null, ex);
          }
          
          System.out.println(date);
          

        【讨论】:

          猜你喜欢
          • 2011-09-08
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-04-23
          • 1970-01-01
          • 2015-09-17
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多