Home > Back-end >  Converting date to string is not working well
Converting date to string is not working well

Time:07-21

I'm trying to post date data by using retrofit2 in Android Java. I don't know why it happened. Because I wrote code to convert Date to String. Also when debugging, I could check string result.

  • My Goal: "created_time": "2022-07-15 18:17:20"

  • Result: "created_time": "java.text.SimpleDateFormat@4f76f1a0"

Date dt = new Date();
SimpleDateFormat date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Log.d("DATE",date.format(dt).toString());
String createdTime = date.format(dt);

postData(useridx, deviceid, correlation, tempvalue, intensity, shutterspeed, createdTime.toString());

private void postData(String user_idx, String device_id, String correlation, String value, String intensity, String shutterspeed, String created_time) {

        // on below line we are creating a retrofit
        // builder and passing our base url
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("URL")
                // as we are sending data in json format so
                // we have to add Gson converter factory
                .addConverterFactory(GsonConverterFactory.create())
                // at last we are building our retrofit builder.
                .build();
        // below line is to create an instance for our retrofit api class.
        RetrofitAPI retrofitAPI = retrofit.create(RetrofitAPI.class);

        // passing data from our text fields to our modal class.
        MeasureDataClass modal = new MeasureDataClass(user_idx, device_id, correlation, value, intensity, shutterspeed, created_time);

        // calling a method to create a post and passing our modal class.
        Call<MeasureDataClass> call = retrofitAPI.createPost(modal);

        // on below line we are executing our method.
        call.enqueue(new Callback<MeasureDataClass>() {
            @Override
            public void onResponse(Call<MeasureDataClass> call, Response<MeasureDataClass> response) {

                // we are getting response from our body
                // and passing it to our modal class.
                MeasureDataClass responseFromAPI = response.body();
            }

            @Override
            public void onFailure(Call<MeasureDataClass> call, Throwable t) {
                // setting text to our text view when
                // we get error response from API.
                Log.e("POST RESPONSE ERROR", "POST ERROR");
            }
        });
    }

 

CodePudding user response:

You should pass createdTime.format(date) instead of createdTime.toString().

CodePudding user response:

createdTime is already String. The code that you posted cannot cause the text that you've shown. You will get the text java.text.SimpleDateFormat@4f76f1a0 only if you pass the SimpleDateFormat object which you named date.

Make sure that the code you've provided is actually the one that is running. Make sure everything is recompiled successfully.

  • Related