Android: 无法从字符串值构造 java.sql.Timestamp 的实例
Android: Can not construct instance of java.sql.Timestamp from String value
我开发了一个 REST API,我正在尝试使用 Android 连接到它。下面是我的代码。
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"))
所以似乎转换 Timestamp
有问题。在我的 Bean
class 中,下面是 Timestamp
的定义方式。
public void setDateCreated(Timestamp dateCreated) {
this.dateCreated = dateCreated;
}
public Timestamp getLastUpdated() {
return lastUpdated;
}
在我的 restCall()
方法中,我调用 Common.getSQLCurrentTimeStamp()
来生成时间戳。下面是那个方法。
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()
方法时,为什么会出现此 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"))
错误?我该如何解决?
我自己解决了这个问题。 Retrofit 2
正在使用 GSON,因此您必须手动提供日期时间转换器
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();
我开发了一个 REST API,我正在尝试使用 Android 连接到它。下面是我的代码。
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"))
所以似乎转换 Timestamp
有问题。在我的 Bean
class 中,下面是 Timestamp
的定义方式。
public void setDateCreated(Timestamp dateCreated) {
this.dateCreated = dateCreated;
}
public Timestamp getLastUpdated() {
return lastUpdated;
}
在我的 restCall()
方法中,我调用 Common.getSQLCurrentTimeStamp()
来生成时间戳。下面是那个方法。
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()
方法时,为什么会出现此 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"))
错误?我该如何解决?
我自己解决了这个问题。 Retrofit 2
正在使用 GSON,因此您必须手动提供日期时间转换器
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();