Java日期格式化-线程安全
来源:互联网 发布:剑灵极品龙女数据 编辑:程序博客网 时间:2024/06/05 16:12
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package my.util.DateTimeTools;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
/**
*
* @author Administrator
*/
public class SafeDateFormat {
final static Map<String, ThreadLocal<DateFormat>> threadLocalPool = new HashMap<>();
final static ThreadLocal<DateFormat> DefaultThreadLocal = new ThreadLocal<DateFormat>() {
@Override
protected synchronized DateFormat initialValue() {
return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
}
};
static {
threadLocalPool.put("yyyy-MM-dd HH:mm:ss", DefaultThreadLocal);
}
public static Date parse(String dateStr) throws ParseException {
return DefaultThreadLocal.get().parse(dateStr);
}
public static Date parse(final String format, String dateStr) throws ParseException {
ThreadLocal<DateFormat> threadLocal = threadLocalPool.get(format);
if (threadLocal == null) {
threadLocal = new ThreadLocal<DateFormat>() {
@Override
protected synchronized DateFormat initialValue() {
return new SimpleDateFormat(format);
}
};
threadLocalPool.put(format, threadLocal);
}
return threadLocal.get().parse(dateStr);
}
public static String format(Date date) {
return DefaultThreadLocal.get().format(date);
}
public static String format(final String format, Date date) {
ThreadLocal<DateFormat> threadLocal = threadLocalPool.get(format);
if (threadLocal == null) {
threadLocal = new ThreadLocal<DateFormat>() {
@Override
protected synchronized DateFormat initialValue() {
return new SimpleDateFormat(format);
}
};
threadLocalPool.put(format, threadLocal);
}
return threadLocal.get().format(date);
}
}
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package my.util.DateTimeTools;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
/**
*
* @author Administrator
*/
public class SafeDateFormat {
final static Map<String, ThreadLocal<DateFormat>> threadLocalPool = new HashMap<>();
final static ThreadLocal<DateFormat> DefaultThreadLocal = new ThreadLocal<DateFormat>() {
@Override
protected synchronized DateFormat initialValue() {
return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
}
};
static {
threadLocalPool.put("yyyy-MM-dd HH:mm:ss", DefaultThreadLocal);
}
public static Date parse(String dateStr) throws ParseException {
return DefaultThreadLocal.get().parse(dateStr);
}
public static Date parse(final String format, String dateStr) throws ParseException {
ThreadLocal<DateFormat> threadLocal = threadLocalPool.get(format);
if (threadLocal == null) {
threadLocal = new ThreadLocal<DateFormat>() {
@Override
protected synchronized DateFormat initialValue() {
return new SimpleDateFormat(format);
}
};
threadLocalPool.put(format, threadLocal);
}
return threadLocal.get().parse(dateStr);
}
public static String format(Date date) {
return DefaultThreadLocal.get().format(date);
}
public static String format(final String format, Date date) {
ThreadLocal<DateFormat> threadLocal = threadLocalPool.get(format);
if (threadLocal == null) {
threadLocal = new ThreadLocal<DateFormat>() {
@Override
protected synchronized DateFormat initialValue() {
return new SimpleDateFormat(format);
}
};
threadLocalPool.put(format, threadLocal);
}
return threadLocal.get().format(date);
}
}
阅读全文
0 0
- Java日期格式化-线程安全
- Java线程安全的日期格式化工具类
- Java日期计算类(线程安全)
- java 日期格式化 字符串<->日期
- JAVA格式化时间日期
- java日期格式化
- JAVA格式化日期
- Java日期格式化
- Java日期格式化
- JAVA格式化日期时间
- Java日期格式化
- java日期格式化
- java日期格式化
- java日期格式化
- JAVA中的日期格式化
- Java日期格式化方法
- java格式化日期类型
- JAVA格式化时间日期
- POJ1703 并查集
- HTTP状态码详解
- 消息中间件-activemq安全机制
- 【剑指offer-解题系列(36)】 数组中的逆序对
- 远程桌面控制软件
- Java日期格式化-线程安全
- HDU2454 判定简单图
- 前端常问问题总结
- 统一监控报警平台架构设计思路
- mysql 5.6.36 win7 32位免安装版配置
- 【剑指offer-解题系列(37)】 两个链表的第一个公共结点。
- POJ1753 枚举
- echarts模拟highcharts实现折线图的虚实转换
- C#对话框 保存对话框、打开对话框的实现、颜色文件框、字体文本框