如何将连接的时间和日期字符串转换为日期?

时间:2016-04-22 07:38:54

标签: android date simpledateformat

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

提醒日期格式为:

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) {}
}

我没有获得日期值。试图在日志中也在吐司中打印日期值,但它没有显示任何内容。出了什么问题?

4 个答案:

答案 0 :(得分:1)

你需要改变两件事:

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

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

答案 1 :(得分:1)

如果您合并格式,那么

用于连接日,那么您的格式为

 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) {}
}

答案 2 :(得分:1)

  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);
    

答案 3 :(得分:0)

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

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) {}
}