Java String类转Date类

Posted on 2022-11-24 21:20  梦中千秋  阅读(354)  评论(0)    收藏  举报
  /**
     * 类型转换 String => Date
     *
     * @param time 字符串日期
     * @return Date
     */
    private static Date StringToDate(String time) {
        if (time == null) return null;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        try {
            return sdf.parse(time);
        } catch (ParseException e) {
            //字符串格式不匹配,抛出异常->"参数错误"
            e.printStackTrace();
            //throw new APIException(ResponseCode.PARAM_ERROR); 自定义异常
        }
    }

通过修改 yyyyMMdd 可自定义输入的字符串日期格式.