用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. ORACLE查看数据库已安装补丁

    cd $ORACLE_HOME ./opatch lsinventory :}

  2. query 中 radio选中小技巧

    在php中经常,经常要用到radio选中按钮,下次再登录时默认记录用户选中的选项,在PHP判断的时候: 在input中不能加checked=“<?php ;?>”:否则失效

  3. div阴影

    .box-shadow{ //Firefox4.0- -moz-box-shadow:投影方式 X轴偏移量 Y轴偏移量阴影模糊半径 阴影扩展半径 阴影颜色; //Safariand Google ch ...

  4. 反射式DLL注入--方法

    使用RWX权限打开目标进程,并为该DLL分配足够大的内存. 将DLL复制到分配的内存空间. 计算DLL中用于执行反射加载的导出的内存偏移量. 调用CreateRemoteThread(或类似的未公开的 ...

  5. Prometheus Node_exporter 之 Network Traffic Detail

    Network Traffic Detail /proc/net/dev 1. Network Traffic by Packets type: GraphUnit: packets/secLabel ...

  6. MySQL基础之 日期时间函数

    基础日期函数和时间函数 1.CURDATE()函数:返回当前只带有年月日格式的日期 2.CURTIME()函数:返回当前只带有时分秒格式的时间 3.NOW()函数:返回当前日期和时间 4.UNIX_T ...

  7. 读高性能JavaScript编程 第一章

    草草的看完第一章,虽然看的是译文也是感觉涨姿势了, 我来总结一下: 由于 大多数浏览器都是 single process 处理 ui updatas and js execute 于是产生问题: js ...

  8. October 17th 2017 Week 42nd Tuesday

    We execuse our sloth under the pretext of difficulty. 我们常以困难为由,作为懒惰的借口. The process of my system-tra ...

  9. SDN2017 第四次作业

    1.阅读 了解SDN控制器的发展 http://www.sdnlab.com/13306.html http://www.docin.com/p-1536626509.html 了解ryu控制器 ht ...

  10. SVN 图标不显示的解决办法

    SVN 的图标没办法显示了.经搜索,发现需要修改注册表 HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Sh ...