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

How to convert concatenated time and date string into date?

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

警报日期格式为:

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 中,但它没有显示任何内容。怎么了?

尝试在日期和时间之间添加 space

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

你需要改变两件事:

  • 在连接日期
  • 时添加一个 space
  • 更改您的 SimpleDateFormat(也将 DateFormatspace 连接):

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

对于连接日,如果您合并格式,那么您的格式是

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

现在用space添加日期和时间所以它是这样的格式

 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 use 中编写此代码,

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) {}
}
  1. 更改 SimpleDateFormat 参数以适合输入。
  2. 在'mAlertDate'和'mAlertTime'之间添加一个space。
  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);