在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字符串为时间一次被坑经历的更多相关文章

  1. C# Oracle 时间字符串转时间类型

    C# 字符串转时间类型 yyyy-MM-dd HH:mm:ss  yyyy-MM-dd hh:mm:ss d 月中的某一天.一位数的日期没有前导零. dd 月中的某一天.一位数的日期有一个前导零. d ...

  2. python 下字符串格式时间比较

    python 下有多个有关时间的模块,分别是time.datetime.calendar,今天重点讨论下time写法. 其中time模块,主要有以下方法: ltime=time.time() 获取当前 ...

  3. postgresql数据库的 to_date 和 to_timestamp 将 字符串转换为时间格式

    数据库中:字符串 转换为 时间格式 二者区别: to_data 转换为 普通的时间格式        to_timestamp 转换可为 时间戳格式出错场景: 比较同一天 日期大小的时候,很容易出错 ...

  4. JQuery 字符串转时间格式

    //字符串转时间格式 function getDate(strDate) { var date = eval('new Date(' + strDate.replace(/\d+(?=-[^-]+$) ...

  5. c# 工具类(字符串和时间,文件)

    using System; using System.IO; using System.Text.RegularExpressions; using System.Windows.Browser; n ...

  6. java字符串、时间大小比较

    package mytest; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util ...

  7. java字符串和时间类型的相互转换

    整理的时间正则可能不全 /****** * * 是以"-" 为分隔符的 * * * * ******/ // 2012-12-03 04:07:34 reg = "\\d ...

  8. Java 字符串和时间互相转化 +时间戳

    一:字符串转换成date String datatime="2015-09-22 15:16:48"; SimpleDateFormat form = new SimpleDate ...

  9. 字符串格式时间转Date格式

    /** * 字符串时间格式转 Date 格式 * @param strDate * @return */ public static Date getDateTimeByStringTime(Stri ...

随机推荐

  1. STS项目html文件中文乱码解决

    解决方案: windows -- perferences -- encoding,设置成utf-8 步骤一:Content Types 步骤二:Workspace 步骤三:JSP Files

  2. 用Navicat Premium 操作MySQL数据库

    1. 用Navicat来查看MySQL数据库        打开Navicat Premium–>[连接]–>[MySQL]–>[连接名:新建数据库的名字,此处为“本地”]:[主机: ...

  3. html字符转义

    常用表: No. 文字表記 10進表記 16進表記 文字   Comment 001 " " " """   quotation mark ...

  4. java 泛型 精析

      Created by Marydon on 1.概述 泛型是Java SE 1.5的新特性,泛型的本质是参数化类型,也就是说所操作的数据类型被指定为一个参数: 这种参数类型可以用在类.接口和方法的 ...

  5. Zuul使用Ribbon配置自动重试

    spring cloud的版本不断演进,导致很多配置的配置方式不断改变,有时某个配置在一个版本里面默认是true,后边一升级默认成了false,这点让人有点不爽. 言归正传 0.所使用版本 sprin ...

  6. esp8266烧录Html文件,实现内置网页控制设备!

    代码地址如下:http://www.demodashi.com/demo/14321.html 一.前言: 这个月也快结束了,时间真快,我服务器知识自学依然在路途中,这几天听到热点网页配置esp826 ...

  7. NVIDIA PureVideo Decoder解码器注册码

    http://www.amznz.com/nvidia-purevideo-decoder/ 重装系统后当然得装终极解码来看高清电影,这次为了给喜欢看HD影片的朋友,特意奉上NVIDIA7以上显卡的N ...

  8. duplicate files during packaging of apk

    OSChina Android APP 导入到Android Studio中不能运行,发现一小插曲. 主要实现了开源中国社区 OSC Android 客户端项目源码通过Gradle方式编译 在 And ...

  9. PO_标准内部请购内部采购单抛转订单模组(流程)

    2014-06-03 Created By BaoXinjian

  10. CURL实现HTTP的GET POST方法

    Curl是Linux下一个非常强大的http命令行工具,其功能十分强大. 一.CURL对HTTP的常规訪问 1. 訪问站点 $ curl http://www.linuxidc.com 回车之后.ww ...