0

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

 
Jaeseo Lee
  • 172
  • 10
  • 1
    show how you call `postData` – Autocrab Jul 21 '22 at 08:59
  • At some point you are calling `date.toString()` but you actually want `createdTime` there. An ideal candidate for that mixup would be in the call of that method, so I agree with @Autocrab. – f1sh Jul 21 '22 at 09:02
  • @Autocrab I put the code how to call it. Thanks! – Jaeseo Lee Jul 21 '22 at 09:15
  • 1
    createdTime variable in your code is already a string value. So you don't need to add toString() to that. – DB377 Jul 21 '22 at 09:21
  • Consider not using `Date` and `SimpleDateFormat`. Those classes are troublesome and long outdated. Look into java.time, the modern Java date and time API. And if for Android API level lower than 26 additionally look into core library desugaring. – Ole V.V. Jul 21 '22 at 10:34
  • Probably related: [How do I print my Java object without getting "SomeType@2f92e0f4"?](https://stackoverflow.com/questions/29140402/how-do-i-print-my-java-object-without-getting-sometype2f92e0f4) – Ole V.V. Jul 21 '22 at 10:36
  • Using the `Date` class and naming a variable that is *not* a `Date` with the name `date` is confusing -- it could also possibly confuse yourself? I recommend better and more consistent naming. Both for yourself and for those users on Stack Overflow that are supposed to read and understand your code. – Ole V.V. Jul 21 '22 at 10:38
  • [I cannot reproduce (this is a link).](https://ideone.com/Ok5hc4) – Ole V.V. Jul 21 '22 at 13:51
  • @OleV.V. thanks for your help. right. also I didnt see that objectType things when I debugged. I will try to apply your related link. Thanks! – Jaeseo Lee Jul 22 '22 at 06:19
  • @OleV.V. Right. I will write a code with better naming. thanks! – Jaeseo Lee Jul 22 '22 at 06:19

2 Answers2

0

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

Autocrab
  • 3,474
  • 1
  • 15
  • 15
  • I can't check now, but is it not enough to just pass `createdTime`, as its definition already uses a `format`? One of those should be redundant. – Dávid Laczkó Jul 21 '22 at 09:39
  • `createdTime` is instance of `SimpleDateFormat` class. Calling `toString()` on it will result `java.text.SimpleDateFormat@4f76f1a0`. It's string represenation of this instance. But you need formatted date. So just call `format`and pass it to function – Autocrab Jul 21 '22 at 09:44
  • `createdTime` is defined as `String` - you lost me, I think you should clarify. – Dávid Laczkó Jul 21 '22 at 09:47
  • `toString()` and `format()` both returns `String` object. – Autocrab Jul 21 '22 at 09:50
  • Yes, so `createdTime` is NOT instance of `SimpleDateFormat`, right? And this goes back to my first question. – Dávid Laczkó Jul 21 '22 at 09:53
0

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.

Mor Blau
  • 420
  • 3
  • 15