Android: Can not construct instance of java.sql.Timestamp from String value

☆樱花仙子☆ 提交于 2019-12-08 01:06:47

问题


I have developed a REST API and I am trying to connect to it using Android. Below is my code.

private void restCall()
    {

        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        YourEndpoints request = retrofit.create(YourEndpoints.class);



        SetupBean setupBean = new SetupBean();
            setupBean.setIdPatient(1);
            setupBean.setCircleType(1);
            setupBean.setFamiliarity(1);
            setupBean.setValence(2);
            setupBean.setArousal(3);
            setupBean.setDateCreated(Common.getSQLCurrentTimeStamp());
            setupBean.setLastUpdated(Common.getSQLCurrentTimeStamp());

        Call<ResponseBody> yourResult = request.insertSetup(setupBean);
        yourResult.enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {

                try {
                    Log.d("MainActivity", "RESPONSE: " + response.errorBody().string());
                }
                catch(Exception e)
                {
                    e.printStackTrace();
                }

            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {
                try {
                    t.printStackTrace();
                    Log.d("MainActivity", "RESPONSE: "+"FAILED");
                }
                catch(Exception e)
                {
                    e.printStackTrace();
                }
            }

        });


    }

When I run this, it shows me the below error

Can not construct instance of java.sql.Timestamp from String value 'Jun 9, 2016 4:24:37 PM': not a valid representation (error: Failed to parse Date value 'Jun 9, 2016 4:24:37 PM': Can not parse date "Jun 9, 2016 4:24:37 PM": not compatible with any of standard forms ("yyyy-MM-dd'T'HH:mm:ss.SSSZ", "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", "EEE, dd MMM yyyy HH:mm:ss zzz", "yyyy-MM-dd"))

So it seems like there is an issue with converting the Timestamp. In my Bean class, below is how the Timestamp are defined.

public void setDateCreated(Timestamp dateCreated) {
        this.dateCreated = dateCreated;
    }

    public Timestamp getLastUpdated() {
        return lastUpdated;
    }

In my restCall() method, I am calling a Common.getSQLCurrentTimeStamp() to generate the timestamp. Below is that method.

public static Timestamp getSQLCurrentTimeStamp()
    {
        java.util.Date date = new java.util.Date();
        Timestamp t = new Timestamp(date.getTime());
        System.out.println(t);
        return t;
    }

So, when I run the restCall() method, why am I getting this Can not construct instance of java.sql.Timestamp from String value 'Jun 9, 2016 4:24:37 PM': not a valid representation (error: Failed to parse Date value 'Jun 9, 2016 4:24:37 PM': Can not parse date "Jun 9, 2016 4:24:37 PM": not compatible with any of standard forms ("yyyy-MM-dd'T'HH:mm:ss.SSSZ", "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", "EEE, dd MMM yyyy HH:mm:ss zzz", "yyyy-MM-dd")) error? How can I solve it?


回答1:


I fixed this by my self. Retrofit 2 is using GSON, so you have to give the date time converter manually

Gson gson = new GsonBuilder()
                .setDateFormat("yyyy-MM-dd'T'HH:mm:ss")
                .create();

        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(URL)
                .addConverterFactory(GsonConverterFactory.create(gson))
                .build();


来源:https://stackoverflow.com/questions/37724511/android-can-not-construct-instance-of-java-sql-timestamp-from-string-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!