无法将 MMM、dd yyyy 的日期转换为 Android 中的 dd/MM/yyyy

Cannot convert a date of MMM, dd yyyy to a dd/MM/yyyy in Android

我正在尝试将 MMM、dd yyyy 的日期转换为 dd/mm/yyyy 的格式以更新日期选择器,但我在以下解析方法中遇到错误:

SimpleDateFormat format = new SimpleDateFormat("MMM, dd yyyy");
Date newDate = format.**parse**(strCurrentDate);

format = new SimpleDateFormat("dd/MM/yyyy");
String date = format.format(newDate);

 String str[] = date.split("/");
 int myday = Integer.parseInt(str[0]);
 int mymonth = Integer.parseInt(str[1]);
 int myyear = Integer.parseInt(str[2]);
 dp.updateDate(myday,mymonth,myyear);

我这里做错了什么?我应该用 try / catch 附上它吗? 谢谢

您需要首先使用 SimpleDateFormatter(SDF) 解析您的字符串,如下所示

String strDate = "MM, dd yyyy";
SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy");
Date dateStr = formatter.parse(strDate);

使用它来将字符串解析为日期,然后您的其他 SDF 将该日期转换为您想要的格式。

这肯定有效。

 String inputPattern = "MMM, dd yyyy";
        String outputPattern = "dd/MM/yyyy";
        SimpleDateFormat inputFormat = new SimpleDateFormat(inputPattern, Locale.ENGLISH);
        SimpleDateFormat outputFormat = new SimpleDateFormat(outputPattern,Locale.ENGLISH);

        Date date = null;
        String str = null;

        try {
            String strCurrentDate = "Sep, 1 2016";
            date = inputFormat.parse(strCurrentDate);// it's format should be same as inputPattern
            str = outputFormat.format(date);
            Log.e("Log ","str "+str);
        } catch (ParseException e) {
            e.printStackTrace();
        } catch (java.text.ParseException e) {
            e.printStackTrace();
        }