Skip to content Skip to sidebar Skip to footer

Converting Json Date Value Into Format "day, Month"

I have an SQL database linked to a web service. The web service will send JSON data to my android application using JAVA. The date was stored as DATETIME in SQL, and displayed as '

Solution 1:

Try

// format dateStringdt="2017-11-14T00:00:00";
// old SimpleDateFormat SimpleDateFormatsdf1=newSimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss", Locale.ENGLISH);
// new SimpleDateFormat SimpleDateFormatsdf2=newSimpleDateFormat("dd MMMM", Locale.ENGLISH);
Datedate=null;
    try {
    date = sdf1.parse(dt);
    StringnewDate= sdf2.format(date);
    System.out.println(newDate);
    Log.e("Date", newDate);
} catch (ParseException e) {
    e.printStackTrace();
}

OUTPUT

enter image description here

Solution 2:

Use java.text.SimpleDateFormat as this tutorial.

publicstaticStringconvertDate(String date) {
    SimpleDateFormat fromFormat = newSimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
    try {
        Date dt = fromFormat.parse(date);
        returnnewSimpleDateFormat("dd MMMM").format(dt);
    } catch (ParseException e) {
        return"";
    }
}

Solution 3:

Calendar instance return day, month and year, below code will return month and date

JsonArrayRequest jsonRequest = newJsonArrayRequest(Request.Method.GET, url, null, newResponse.Listener<JSONArray>() {
            @OverridepublicvoidonResponse(JSONArray response) {
                for (int j = 0; j < response.length(); j++) {
                    try {
                        JSONObject temp=response.getJSONObject(j);
                        String date = temp.getString("Date")
                        Log.v("Date", date);
                        SimpleDateFormat sdf = newSimpleDateFormat("MM/dd/yyyy HH:mm:ss");
                        Date parse = sdf.parse(date);//date variable your json valueCalendar c = Calendar.getInstance();
                        c.setTime(parse);
                        System.out.println(c.get(Calendar.MONTH) + c.get(Calendar.DATE));

                    catch (JSONException e) {
                        e.printStackTrace();

                    }

Happy coding!!

Solution 4:

Use SimpleDateFormat for convert in desired format like :

SimpleDateFormat Dateformat = new SimpleDateFormat("dd-MMMM-yyyy hh:mm a", Locale.US);
Datedate =   Dateformat.parse(YourDateString);
String StringDate = Dateformat .format(date);
String MyNewDate= StringDate.split(" ")[0]+ StringDate.split(" ")[1];

Post a Comment for "Converting Json Date Value Into Format "day, Month""