关于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 ...
随机推荐
- HTTP请求响应码
① 客户方错误 100 继续 101 交换协议 ② 成功 200 OK 201 已创建 202 接收 203 非认证信息 204 无内容 205 重置内容 206 部分内容 ③ 重定向 300 多路选 ...
- linux - 文件夹、文件默认属性: umask使用
一 权限掩码umask umask是chmod配套的.总共为4位(gid/uid,属主.组权,其他用户的权限),只是通经常使用到的是后3个.比如你用chmod 755 file(此时这文件的权限是属主 ...
- Google Hack的一些整理
这里是一些关于Google Hack方面的整理 黑客专用信息和资料搜索地址为: http://www.google.com/custom?hl=xx-hacker 这里是google关键字的用法,要设 ...
- JSTL fmt:formatNumber 数字、货币格式化
<fmt:formatNumber value="12.34" pattern="#0.00" /> 12.34 保留小数点后两位数 <fmt ...
- js 面向对象式编程
1.声明一个函数,在函数内进行初始化操作,,函数不能有返回值2.把需要的参数传递进去,参数最好以对象形式传入,如果有默认的设置默认参数3.把传入的参数都保存到对象的属性上面4.把初始化操作中需要用到的 ...
- html5 效果 按下鼠标数值自动增长
<!doctype html> <html> <head> <style> * { margin:0; padding:0; } div { margi ...
- 全面提升WordPress前台和后台的 打开速度的方案
装好WordPress之后,准备想访问自己的网站,或是登入后台的时候,却发现,这个速度不敢恭维,即使是本地话的程序,也是慢慢的.操作起来也要挺久.那下面我们来解决一下这个问题,提升WordPress的 ...
- Apache Hadoop 3.0新版本介绍及未来发展方向
过去十年,Apache Hadoop从无到有,从理论概念演变到如今支撑起若干全球最大的生产集群.接下来的十年,Hadoop将继续壮大,并发展支撑新一轮的更大规模.高效和稳定的集群. 我们此次将向大家全 ...
- Web Service基础——基础概念
1. Web Service基本概念 Web Service(Web服务)是一种远程调用技术,他的作用就是从远程系统中获取业务数据.具体来说,Web Service可以让你的网站使用其他网站的资源,比 ...
- Linux-Linux下安装redis报错"undefined reference to__sync_add_and_fetch_4"解决办法
如果出现这种错误可以在make的时候加上CFLAGS="-march=i686" 即 make CFLAGS="-march=i686" ----------- ...