In the process of date, conversion encountered this problem, very angry
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//Set username and perform time conversion
for (int i = 0; i < purchasedFundsList.size() ; i++) {
purchasedFundsList.get(i).setUserName(userName);
purchasedFundsList.get(i).setCreateTime(sdf.format(purchasedFundsList.get(i).getCreateTime()));
}
Check the API and find a problem. The parameter of format (date) method can only be of date type, but I passed string type, so the parameter type of the method is wrong
When through a small tool conversion, the problem is solved
sdf.format(TimeUtil.StringToDate(purchasedFundsList.get(i).getCreateTime()));
/**
* Convert string time format to Date time format with parameter String type
* For example string time: "2017-12-15 21:49:03"
* converted date time: Fri Dec 15 21:49:03 CST 2017
* @param datetime type is String
* @return
*/
public static Date StringToDate(String datetime){
SimpleDateFormat sdFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = new Date();
try {
date = sdFormat.parse(datetime);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return date;
}
Solve the problem!
Similar Posts:
- [How to Solve] Failed to convert value of type ‘java.lang.String’ to required type ‘java.util.Date’;
- Get the time value from the database and report an error: Java sql. Timestamp cannot be cast to java. lang.Long
- On the non thread safety problem of simpledateformat and its solution
- ValueError: day is out of range for month [How to Solve]
- ValueError: day is out of range for month [How to Solve]
- Springboot uses the Datetimeformat (pattern = “yyyy MM DD HH: mm: SS”) annotation to automatically convert the string to date type error
- [Solved] No converter found for return value of type: class org.json.JSONObject
- TypeError: Object of type ‘datetime‘ is not JSON serializable [Solved]
- C# String was not recognized as a valid DateTime [How to Solve]
- MySQL from_ Unixtime() and UNIX_ The difference of timestamp() function