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;
        }
  • 相关阅读:
    将excel里的有效数据提取出来
    基础二
    状态码
    基础(一)
    爬虫页面
    交集,并集,差集,函数
    魔法和运算器
    Java接口
    Java封装
    Java抽象类
  • 原文地址:https://www.cnblogs.com/tony-yang-flutter/p/3340231.html
Copyright © 2011-2022 走看看