java线程安全类时间处理

SimpleDateFormat是线程不安全,
关于时间处理推荐进行如下,如果是jdk低于1.7用:
public final static String DATE_PATTERN = "yyyy-MM-dd";
private static ThreadLocal threadLocal = new ThreadLocal() {
    @Override
    protected DateFormat initialValue() {
        return new SimpleDateFormat(DATE_TIME_PATTERN);
    }
};

/**
 * 线程安全的转换
 *
 * @param dateStr
 * @return
 * @throws ParseException
 */
public static Date parseSync(String dateStr) throws ParseException {
    return threadLocal.get().parse(dateStr);
}


/**
 * 线程安全的时间工具类
 *
 * @param date
 * @return
 */
public static String formatSync(Date date) {
    return threadLocal.get().format(date);
}


jdk1.8提供新的时间处理,是线程安全的:
public static Date parseSync8(String dateStr, String pattern) {
    if (StringUtils.isEmpty(pattern)) {

        pattern = DATE_PATTERN;
    }
    java.time.format.DateTimeFormatter formatter = java.time.format.DateTimeFormatter.ofPattern(pattern);

    java.time.LocalDate date = java.time.LocalDate.parse(dateStr, formatter);

    ZoneId zoneId = ZoneId.systemDefault();
    ZonedDateTime zdt = date.atStartOfDay(zoneId);

    return Date.from(zdt.toInstant());
}

public static String formatSync8(Date date, String pattern) {
    if (StringUtils.isEmpty(pattern)) {

        pattern = DATE_PATTERN;
    }

    java.time.format.DateTimeFormatter formatter = java.time.format.DateTimeFormatter.ofPattern(pattern);
    ZoneId zoneId = ZoneId.systemDefault();
    LocalDateTime now = date.toInstant().atZone(zoneId).toLocalDateTime();

    return now.format(formatter);

}

你可能感兴趣的:(java)