zoukankan      html  css  js  c++  java
  • Java java.text.ParseException: Unparseable date

    用java将字符串转换成Date类型是,会出现java.text.ParseException: Unparseable date异常。

    例如下面的这段代码就会出现上面的异常:

    public boolean ratherDate(String date){
            try{
                SimpleDateFormat formate = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
                Date todayDate = formate.parse(formate.format(new Date()));
                Date targeDate = formate.parse(date);
                           if(Math.abs(((targeDate.getTime() - todayDate.getTime())/(24*3600*1000))) >= 0){
                    return true;
                }
                return false;
            }catch(Exception e){
                e.printStackTrace();
            }
            return false;
        }

    解决办法有两种:

    一、Date targetDate = formate.parse(date.toString());

    二、Date targetDate = (Date)formate.parseObject(date);

    到此为止,问题解决

    大家可以把下面这段代码copy上去试试看。

    public boolean ratherDate(String date){
            try{
                SimpleDateFormat formate = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
                Date todayDate = (Date)formate.parseObject(formate.format(new Date()));
                Date targeDate = (Date)formate.parseObject(date);
                //如果最晚预订时间大于当前日期则允许订购当日票
                if(Math.abs(((targeDate.getTime() - todayDate.getTime())/(24*3600*1000))) >= 0){
                    return true;
                }
                return false;
            }catch(Exception e){
                e.printStackTrace();
            }
            return false;
        }
  • 相关阅读:
    springboot缓存-Ehcache
    springboot+spring data jpa 多数据源配置
    vue+element 上传图片控件
    springboot下载文件
    easyPoi导入带图片的excel
    内外网同时使用(宽带内网无线内网)
    mysql 8.0 安装
    搭建一个Vue前端项目
    mybatis反向代理自动生成mapper文件
    【1】idea Live Templates
  • 原文地址:https://www.cnblogs.com/tony-yang-flutter/p/3340231.html
Copyright © 2011-2022 走看看