关于Parse字符串为时间一次被坑经历
在Java代码中发现一个bug,就是本来更新为时间的内容更新为一些奇怪的内容,比如20819这种形式,本来更新的时间都是近期不会超过一年,
为什么会出现这种情况,非常奇怪,遂调试下代码,跟踪发现要匹配的字符串内容和预想的日期格式不符合,代码处理这种情况是抛出异常,
然后用今天的日期替代,结果没成功,代码大概如下:
String dt = "20160901";
SimpleDateFormat dateFm = new SimpleDateFormat("yyyyMM");
Date strSuffix = null;
try{
strSuffix = dateFm.parse(dt);
} catch(Exception e){
strSuffix = new Date();
e.printStackTrace();
} System.out.println("result date:"+strSuffix.toLocaleString());
按照本来的思路,应该是解析发生异常,然后时间为当前时间,结果打印为:2091-1-1 0:00:00
可见,就算格式和实际的不符合,也不会抛出异常,仔细检查后发现,0901也当做月份来处理,即901,然后除12的话,等于75,再加上年份2016,
刚好是2091年,这个确实和我们的预期不符,所以在做这类转化前最好确认下位数防止这种奇怪的现象。
后来了解到SimpleDateFormat的生成开销比较大,尽量少用,而且不是线程安全的函数,如果网上提供了一个高效的用法:
package com.peidasoft.dateformat; import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date; public class ThreadLocalDateUtil {
private static final String date_format = "yyyy-MM-dd HH:mm:ss";
private static ThreadLocal<DateFormat> threadLocal = new ThreadLocal<DateFormat>(); public static DateFormat getDateFormat()
{
DateFormat df = threadLocal.get();
if(df==null){
df = new SimpleDateFormat(date_format);
threadLocal.set(df);
}
return df;
} public static String formatDate(Date date) throws ParseException {
return getDateFormat().format(date);
} public static Date parse(String strDate) throws ParseException {
return getDateFormat().parse(strDate);
}
}
或者
package com.peidasoft.dateformat; import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date; public class ConcurrentDateUtil { private static ThreadLocal<DateFormat> threadLocal = new ThreadLocal<DateFormat>() {
@Override
protected DateFormat initialValue() {
return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
}
}; public static Date parse(String dateStr) throws ParseException {
return threadLocal.get().parse(dateStr);
} public static String format(Date date) {
return threadLocal.get().format(date);
}
}
说明:使用ThreadLocal, 也是将共享变量变为独享,线程独享肯定能比方法独享在并发环境中能减少不少创建对象的开销。如果对性能要求比较高的情况下,一般推荐使用这种方法。
关于Parse字符串为时间一次被坑经历的更多相关文章
- C# Oracle 时间字符串转时间类型
C# 字符串转时间类型 yyyy-MM-dd HH:mm:ss yyyy-MM-dd hh:mm:ss d 月中的某一天.一位数的日期没有前导零. dd 月中的某一天.一位数的日期有一个前导零. d ...
- python 下字符串格式时间比较
python 下有多个有关时间的模块,分别是time.datetime.calendar,今天重点讨论下time写法. 其中time模块,主要有以下方法: ltime=time.time() 获取当前 ...
- postgresql数据库的 to_date 和 to_timestamp 将 字符串转换为时间格式
数据库中:字符串 转换为 时间格式 二者区别: to_data 转换为 普通的时间格式 to_timestamp 转换可为 时间戳格式出错场景: 比较同一天 日期大小的时候,很容易出错 ...
- JQuery 字符串转时间格式
//字符串转时间格式 function getDate(strDate) { var date = eval('new Date(' + strDate.replace(/\d+(?=-[^-]+$) ...
- c# 工具类(字符串和时间,文件)
using System; using System.IO; using System.Text.RegularExpressions; using System.Windows.Browser; n ...
- java字符串、时间大小比较
package mytest; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util ...
- java字符串和时间类型的相互转换
整理的时间正则可能不全 /****** * * 是以"-" 为分隔符的 * * * * ******/ // 2012-12-03 04:07:34 reg = "\\d ...
- Java 字符串和时间互相转化 +时间戳
一:字符串转换成date String datatime="2015-09-22 15:16:48"; SimpleDateFormat form = new SimpleDate ...
- 字符串格式时间转Date格式
/** * 字符串时间格式转 Date 格式 * @param strDate * @return */ public static Date getDateTimeByStringTime(Stri ...
随机推荐
- UVA 357 Let Me Count The Ways(全然背包)
UVA 357 Let Me Count The Ways(全然背包) http://uva.onlinejudge.org/index.php?option=com_onlinejudge& ...
- 算法笔记_208:第六届蓝桥杯软件类决赛真题(Java语言A组)
目录 1 胡同门牌号 2 四阶幻方 3 显示二叉树 4 穿越雷区 5 切开字符串 6 铺瓷砖 前言:以下代码仅供参考,若有错误欢迎指正哦~ 1 胡同门牌号 标题:胡同门牌号 小明家住在一条胡同里. ...
- 【安卓】给gallery内"控件"挂载事件,滑动后抬起手指时也触发事件(滑动时不应触发)的解决、!
思路: 1.gallery内控件挂载事件(如:onClickListener)的方法类似listview,可直接在baseAdapter.getView内给控件挂载(详细方法百度). 2.貌似没问题, ...
- TOMCAT问题总结
迁移时间--2017年7月9日14:58:12Author:Marydon CreateTime--2016年12月25日21:55:09Author:MarydonTomcat问题总结问题一 A ...
- centos7 开启ftp服务
1.关闭默认防火墙 systemctl stop firewalld.service #停止firewall systemctl disable firewalld.service #禁止firewa ...
- MySQL存储过程中的3种循环【转载】
在MySQL存储过程的语句中有三个标准的循环方式:WHILE循环,LOOP循环以及REPEAT循环.还有一种非标准的循环方式:GOTO,不过这种循环方式最好别用,很容易引起程序的混乱,在这里就不错具体 ...
- 解决python:'ascii' codec can't encode characters in position问题
今天把一个列表转换成字符串输出的时候出现了UnicodeEncodeError: 'ascii' codec can't encode characters in position 32-34: or ...
- AJAX 跨域 CORS 解决方案
本篇文章由:http://xinpure.com/solutions-for-cross-domain-ajax-cors/ 两种跨域方法 在 Javascript 中跨域访问是比较常见的事情 就像现 ...
- Chrome浏览器桌面通知提示设置
版本 24.0.1312.56 m 老版本23.* 桌面通知,也可以由用户在Chrome浏览器中自定义:板手 -> 选项 -> 高级选项 –> 通知 (管理例外情况…).
- javascript高级程序设计第三章
看后总结: 1.区分大小写 2.标识符是有字母下划线$开头,并有字母.下划线.数字.美元符号组成. 3.建议用驼峰法命名标识符. 4.注释: 单行:// 多行: /* */ 5.严格模式: 在js ...