Android:无法从String值构造java.sql.Timestamp的实例 [英] Android: Can not construct instance of java.sql.Timestamp from String value
问题描述
我开发了一个REST API,我正在尝试使用Android连接到它。以下是我的代码。
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();
}
}
});
}
当我运行时,它向我显示以下错误
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"))
因此,转换时间戳
似乎存在问题。在我的 Bean
类中,下面是如何定义 Timestamp
。
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;
}
在我的 restCall()
方法,我调用 Common.getSQLCurrentTimeStamp()
来生成时间戳。以下是该方法。
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;
}
因此,当我运行 restCall()时/ code>方法,为什么我得到这个
无法从字符串值'2016年6月9日下午4:24:37'构造java.sql.Timestamp的实例:不是有效的表示形式(错误:无法解析日期值'2016年6月9日下午4:24:37':无法解析日期2016年6月9日下午4:24:37:与任何标准格式不兼容(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, 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?
推荐答案
我自己解决了这个问题。 Retrofit 2
正在使用GSON,因此您必须手动提供日期时间转换器
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();
这篇关于Android:无法从String值构造java.sql.Timestamp的实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!