从服务器 api 获取日期并转换其格式?

Get date from server api and convert its format?

我目前正在解析来自 PHP 服务器 api 的日期,来自 api 的日期是“1970-04-22”,现在我需要将此日期显示为“04- 22-1970”。我尝试了以下代码,但它没有显示确切的格式。

String date = jsonObject.optString("date"); // output is "1970-04-22"
SimpleDateFormat spf=new SimpleDateFormat("MMM, dd,yyyy hh:mm:ss aaa");
Date newDate=spf.parse(date);
spf= new SimpleDateFormat("MMM dd yyyy");
date = spf.format(newDate);
Log.e("date", String.valueOf(date));
String tempDate = "1970-04-22";
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
        Date date1=null;
        try {
            date1 = simpleDateFormat.parse(tempDate);

        } catch (ParseException e) {
            e.printStackTrace();
        }
        SimpleDateFormat newformat = new SimpleDateFormat("dd-MM-yyyy", Locale.getDefault());
        String dateInReqFormat = newformat.format(date1);

在这些情况下,您必须先将现有格式转换为日期,然后根据需要将日期格式设置为字符串。