Error while using Dateformat.Parse

Asked

Viewed 237 times

0

I take the date and call the changeFormatDataTra

 private void makeJsonObjReq(){
    showProgressDialog();
    int id = ((AppController) this.getApplication()).getID();
    JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.GET,
            Const.URL_JSON_ARRAY_Editar_USU+"/"+id, null,
            new Response.Listener<JSONObject>() {

                @Override
                public void onResponse(JSONObject response) {
                    Log.d(TAG, response.toString());
                //    hideProgressDialog();
                    JSONObject jsonobj = null;
                    int aJsonint = 0;
                    String aJsonString;
                    try {
                        jsonobj = new JSONObject(String.valueOf(response));
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                    try {

                        //String dataresp = trocaFormatoDataTra(jsonobj.getString("nascimento"));
                        String dataresp = trocaFormatoDataTra(jsonobj.getString("nascimento"));
                        JsonNome = jsonobj.getString("nome");
                        JsonNascimento = dataresp;

                        //ADD VALORES CAMPOS
                        NomeTraEd.setText(JsonNome);
                        NascimentoTraEd.setText(JsonNascimento);


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

                }
            }, new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {
            VolleyLog.d(TAG, "Error: " + error.getMessage());
            hideProgressDialog();
        }
    });
    // Adding request to request queue
    AppController.getInstance().addToRequestQueue(jsonObjReq,
            tag_json_obj);

}

I have the following method

 public String trocaFormatoDataTra(String data) {
  //data recebe Dec 31, 1969 12:00:00 AM
    String formatoDeEntrada = "MMM dd, yyyy h:mm:ss";
    String formatoDeSaida = "dd/MM/yyyy";
    SimpleDateFormat dateFormatEntrada = new SimpleDateFormat(formatoDeEntrada);
    SimpleDateFormat dateFormatSaida = new SimpleDateFormat(formatoDeSaida);

    Date dataOriginal = null;
    String dataTrocada = null;

    try {
        //Transforma a String em Date
        dataOriginal = dateFormatEntrada.parse(data);
        //Transforma a Date num String com o formato pretendido
        dataTrocada = dateFormatSaida.format(dataOriginal);
    } catch (ParseException e) {
        //Erro se não foi possível fazer o parse da Data
        e.printStackTrace();
    }
    return dataTrocada;
}

Logcat of error

10-29 15:28:42.430      981-981/spac.com.br.jobbroker W/System.err﹕ 

java.text.ParseException: Unparseable date: "Dec 31, 1969 12:00:00 AM" (at offset 0)
10-29 15:28:42.460      981-981/spac.com.br.jobbroker W/System.err﹕ at java.text.DateFormat.parse(DateFormat.java:626)
10-29 15:28:42.460      981-981/spac.com.br.jobbroker W/System.err﹕ at spac.com.br.jobbroker.Editar.Editar_Perfil_trabalhador.trocaFormatoDataTra(Editar_Perfil_trabalhador.java:319)  //A Linha desse erro é essa dataOriginal = dateFormatEntrada.parse(data);
10-29 15:28:42.460      981-981/spac.com.br.jobbroker W/System.err﹕ at spac.com.br.jobbroker.Editar.Editar_Perfil_trabalhador$2.onResponse(Editar_Perfil_trabalhador.java:209)
10-29 15:28:42.460      981-981/spac.com.br.jobbroker W/System.err﹕ at spac.com.br.jobbroker.Editar.Editar_Perfil_trabalhador$2.onResponse(Editar_Perfil_trabalhador.java:191)
10-29 15:28:42.460      981-981/spac.com.br.jobbroker W/System.err﹕ at com.android.volley.toolbox.JsonRequest.deliverResponse(JsonRequest.java:65)
10-29 15:28:42.472      981-981/spac.com.br.jobbroker W/System.err﹕ at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:99)
10-29 15:28:42.472      981-981/spac.com.br.jobbroker W/System.err﹕ at android.os.Handler.handleCallback(Handler.java:615)
10-29 15:28:42.472      981-981/spac.com.br.jobbroker W/System.err﹕ at android.os.Handler.dispatchMessage(Handler.java:92)
10-29 15:28:42.472      981-981/spac.com.br.jobbroker W/System.err﹕ at android.os.Looper.loop(Looper.java:137)
10-29 15:28:42.472      981-981/spac.com.br.jobbroker W/System.err﹕ at android.app.ActivityThread.main(ActivityThread.java:4745)
10-29 15:28:42.480      981-981/spac.com.br.jobbroker W/System.err﹕ at java.lang.reflect.Method.invokeNative(Native Method)
10-29 15:28:42.480      981-981/spac.com.br.jobbroker W/System.err﹕ at java.lang.reflect.Method.invoke(Method.java:511)
10-29 15:28:42.480      981-981/spac.com.br.jobbroker W/System.err﹕ at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
10-29 15:28:42.490      981-981/spac.com.br.jobbroker W/System.err﹕ at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-29 15:28:42.490      981-981/spac.com.br.jobbroker W/System.err﹕ at dalvik.system.NativeStart.main(Native Method)
  • Thanks again worked out!

1 answer

1


This type of error is frequent when using conversion functions whose behavior depends on the Locale(language/country combination).

The class Simpledateformat, when built with the builder who accepts only one string, uses as Locale the one defined on the device.

In this case, the device that is executing the code, must be set to use the Locale pt_BR or pt_EN.

The problem arises because we are trying to convert a date that has months in English using the class configured to use Portuguese.

The class Simpledateformat has another builder who also receives a Locale.
When passing the Locale indicate which rules the Simpledateformat must use.

In this case instead of using

new SimpleDateFormat(formatoDeEntrada)

we should use

new SimpleDateFormat(formatoDeEntrada, Locale.US) 

Browser other questions tagged

You are not signed in. Login or sign up in order to post.