Error when using DateFormat.Parse

0

I get the date and call the exchangeDataTraFormat

 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;
}

Error logcat

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)
    
asked by anonymous 29.10.2014 / 18:30

1 answer

1

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

The SimpleDateFormat class, when built with the constructor that accepts only a string , uses Locale as the one that is defined on the device.

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

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

The SimpleDateFormat class has another constructor that also receives a Locale . Passing the Locale indicates which rules the SimpleDateFormat should use.

In this case instead of using

new SimpleDateFormat(formatoDeEntrada)

We should use

new SimpleDateFormat(formatoDeEntrada, Locale.US) 
    
29.10.2014 / 19:43