用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;
}

Java java.text.ParseException: Unparseable date的更多相关文章

  1. java.text.ParseException: Unparseable date: "2015-06-09 hh:56:19"

    1.错误描述 [DEBUG:]2015-06-09 16:56:19,520 [-------------------transcation start!--------------] java.te ...

  2. java.text.ParseException: Unparseable date: "2015-06-09 hh:56:19"

    1.错误描写叙述 [DEBUG:]2015-06-09 16:56:19,520 [-------------------transcation start!--------------] java. ...

  3. 异常--java.text.ParseException: Unparseable date

    String d = "2015-05-19" SimpleDateFormat sdf =   new SimpleDateFormat( "yyyy/MM/dd HH ...

  4. 字符串类型日期时间转换为Date类型解析转换异常java.text.ParseException: Unparseable date: “2019-09-27T18:31:31+08:00”

    错误的写法: SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //这里的格式也可以是别 ...

  5. [bug] java.text.ParseException: Unparseable date: "2020-01-01"

    原因 输入的字符串应和目标字符串格式长度都一样 如返回的字符串是:2019-11-11,但解析的格式是:yyyy-MM-dd  HH:mm:ss,导致错误 参考 https://blog.csdn.n ...

  6. java.text.ParseException: Failed to parse date ["未知']

    先把"未知"替换为"" 直接new 出来的Gson 对象是无法解析为""的Date属性的,需要通过GsonBuilder来进行创建 Gson ...

  7. Invocation of init method failed; nested exception is java.text.ParseException: '?' can only be specfied for Day-of-Month or Day-of-Week.

    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'cronTrigger' ...

  8. 很久没来这里,今天的评测java怪东西,左右Date类和时间戳转换

    在发展过程中,经常会遇到利用上课时间.说话的Date类就不得不提时间戳,左右fr=aladdin" target="_blank">的定义大家能够看看网上对时间戳的 ...

  9. Java学习笔记22(Date类、DateFormat类)

    Date,时间和日期类,这里讲util包中的而不是sql包中的 Date:表示特定的时间瞬间,精确到毫秒(1000毫秒=1秒) 时间和日期的操作都基于毫秒值 时间原点:1970年1月1日,0时0分0秒 ...

随机推荐

  1. Intellij IDEA添加项目依赖

    在idea的设置中,存在下面几个标签页 Libraies是向idea中添加的jar或者文件夹进入. module是添加具体的项目依赖,后面的选项为部署时是否忽略. artifacts配置打包方式,需要 ...

  2. Component Interface相关面试题

    下面的列表中的问题很常见,Component Interface是很重要的.你知道基本知识比知道答案更重要. Q:以下陈述是错误的. A:一个Component Interface 可以map多个Pe ...

  3. 使用PeopleSoft 异步操作设置步骤

    在手机端调PS接口或同步数据到中间库等程序中经常用到异步操作. 在主菜单>PeopleTools>集成代理程序>集成设置中添加了服务和服务操作后需要设置以下3步才能完成异步程序: 1 ...

  4. PeopleSoft 多套Web App Prcs交叉访问

    1.Process服务器比较简单,只需要与数据库关联,系统调用时候就会负载均衡,在PSADMIN增加服务器时候,需要选择在"主菜单>PeopleTools>进程调度器>服务 ...

  5. linux下安装mysql(ubuntu0.16.04.1)

    安装步骤: sudo netstat -tap | grep mysql 查看是否已安装 安装mysql:sudo apt-get install mysql-server mysql-client  ...

  6. 网络基础 港湾FlexHammer5010交换机镜像端口配置

    港湾FlexHammer5010交换机镜像端口配置 by:授客 QQ:1033553122 1.登陆港湾交换机FlexHammer5010交换机 方法: telent 交换机ip 输入用户名 输入用户 ...

  7. 解决 web.xml is missing and <failOnMissingWebXml> is set to true 报错

    在学习maven模块化构建项目的时候遇到了如下报错信息: web.xml is missing and <failOnMissingWebXml> is set to true. 这时候需 ...

  8. Ionic模版的使用

    Hybird HTML5 App(移动应用开发)之4.Ionic模版的使用 在Windows系统下使Ionic模版,可以使用之前安装的Git工具中的Git Bash.在Mac系统中可以直接使用系统自带 ...

  9. T-SQL检查停止的复制作业代理,并启动 (转载)

    有时候搭建的复制在作业比较多的时候,会因为某些情况导致代理停止或出错,如果分发代理时间停止稍微过长可能导致复制延期,从而需要从新初始化复制,带来问题.因此我写了一个脚本定期检查处于停止状态的分发代理, ...

  10. MyEclipse总是quick update解决办法

    这个问题的解决办法是关闭自动更新 1. Windows > Preferences > MyEclipse> Community Essentials, 把选项 "Sear ...