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;
        }
  • 相关阅读:
    otto-group-product-classification-challenge(Pytorch处理多分类问题)
    F1值
    win10 安装torch
    win10 安装 scrapy
    头条 街拍
    1029 Median
    欧拉回路
    Pre-Post
    Django 安装for Python3.4
    Python 安装for Windows
  • 原文地址:https://www.cnblogs.com/tony-yang-flutter/p/3340231.html
Copyright © 2011-2022 走看看