[Solved] java.lang.IllegalArgumentException: Cannot format given Object as a Date

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: