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;

    }

 

你可能感兴趣的:(exception)