Java tutorial
//package com.java2s; import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public class Main { private static final String date_format_detail = "yyyy-MM-dd HH:mm:ss"; private static final ThreadLocal<DateFormat> detailDateFormat = new ThreadLocal<DateFormat>() { @Override public DateFormat get() { return super.get(); } @Override protected DateFormat initialValue() { return new SimpleDateFormat(date_format_detail); } @Override public void remove() { super.remove(); } @Override public void set(DateFormat value) { super.set(value); } }; /** * @param dateStr * "yyyy-MM-dd HH:mm:ss" like * @return parsed Date * @throws ParseException */ public static Date parseDetail(String dateStr) { DateFormat dateFormat = getDetailDateFormat(); Date date = null; try { date = dateFormat.parse(dateStr); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } return date; } /** * @return "yyyy-MM-dd HH:mm:ss" like formatter */ public static DateFormat getDetailDateFormat() { DateFormat df = detailDateFormat.get(); if (df == null) { df = new SimpleDateFormat(date_format_detail); detailDateFormat.set(df); } return df; } }