android中的java.text.parseexception不可解析日期

android中的java.text.parseexception不可解析日期,java,android,Java,Android,注:其中dt=2013-03-02,tm=21:54。但我在第九行出错了。我不知道原因是什么。请帮我摆脱这个问题。提前谢谢。问题是您的日期格式不匹配 由于v_date_str是2013-03-02 21:54,您可以使用以下方法匹配其格式: String dt=mDateButton.getText().toString(); String tm =mTimeButton.getText().toString(); try { String format ="dd-MM-yyyy

注:其中dt=2013-03-02,tm=21:54。但我在第九行出错了。我不知道原因是什么。请帮我摆脱这个问题。提前谢谢。

问题是您的日期格式不匹配

由于
v_date_str
2013-03-02 21:54
,您可以使用以下方法匹配其格式:

String dt=mDateButton.getText().toString();
String tm =mTimeButton.getText().toString();
  try { 
    String format ="dd-MM-yyyy hh:mm a";

    DateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.ENGLISH);

    String v_date_str = dt + " " + tm;
    //  String setDate =sdf.format(dt + " " + tm);
    Date v_date = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.ENGLISH).parse(v_date_str );

    DateFormat formatter = null;
    formatter = new SimpleDateFormat("dd-MMM-yyyy");

    Log.d("sset: ", ""+formatter.format(v_date));
  } catch (ParseException e) {
    e.printStackTrace();
  }

问题是您的
DateFormat
模式不匹配

由于
v_date_str
2013-03-02 21:54
,您可以使用以下方法匹配其格式:

String dt=mDateButton.getText().toString();
String tm =mTimeButton.getText().toString();
  try { 
    String format ="dd-MM-yyyy hh:mm a";

    DateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.ENGLISH);

    String v_date_str = dt + " " + tm;
    //  String setDate =sdf.format(dt + " " + tm);
    Date v_date = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.ENGLISH).parse(v_date_str );

    DateFormat formatter = null;
    formatter = new SimpleDateFormat("dd-MMM-yyyy");

    Log.d("sset: ", ""+formatter.format(v_date));
  } catch (ParseException e) {
    e.printStackTrace();
  }