用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. 在学习使用webpack中遇到的错误

    1.局部安装webpack之后使用node_modules/.bin/webpack app/main.js public/bundle.js命令报错      解决方法:      webpack版 ...

  2. 5月7日——采用第三方页面内容,但是顶部title使用自己的

          --------->       由A页面进入我的页面 代码如下: (1)A页面下需要添加的代码 (2)我的页面下需要添加的代码 此处用到的语法为mui框架中的语法,可参照mui官方 ...

  3. 在JavaScript文件中用ajax方法实现省市区的三级联动

    1.JavaScript Document $(document).ready(function(e) { 加载三个下拉列表 $("#sanji").html("< ...

  4. JSP内置对象——response对象

    看一个实例: 运行结果: 出现了一个很奇怪的现象,这个outer对象输出的字符串,跑到顶部去了.这个呢也就说明了response对象获得的writer对象的输出总是前于我们的内置对象.(respons ...

  5. nodejs图像处理模块

    首先是搜索了npm包的性能比较,找到了这篇: https://github.com/ivanoff/images-manipulation-performance 性能最好的当属sharp,由于安装不 ...

  6. Mongodb的入门(6)副本集

    Mongodb的副本集:(有两种情况,主从复制,副本集) 这次我来说一下mongodb的副本集,副本集的出现可以有效的保证mongodb的单点问题,对数据的稳健性有一定辅助作用: MongoDB复制: ...

  7. 6.1 函数的返回值、匿名函数lambda、filter函数、map函数、reduce函数

      函数的返回值: 函数一旦执行到   return,函数就会结束,并会返回return 后面的值,如果不使用显式使用return返回,会默认返回None . return None可以简写为   r ...

  8. python subprocess pipe 实时输出日志

    * test11.py import time print "1" time.sleep(2) print "1" time.sleep(2) print &q ...

  9. Windows 自动更新服务恢复

    之前手贱删除了Windows的自动更新服务,命令: SC DELETE Wuauserv 悲剧的是最近中了[永恒之蓝]病毒,很恼人!杀了毒,最后还是得仰仗Windows的补丁来加固系统.于是想通过SC ...

  10. sql server 用户'sa'登录失败(错误18456)

    转载于:http://thenear.blog.51cto.com/4686262/865544 用户'sa'登录失败(错误18456)解决方案图解     当我们在使用sql server 的时候可 ...