Posted By


keanu on 09/09/10

Tagged


Statistics


Viewed 260 times
Favorited by 0 user(s)

將SimpleDateFormat包裝過的DateUtil


/ Published in: Java
Save to your folder(s)

因為SimpleDateFormat是ThreadSafe且耗資源。


Copy this code and paste it in your HTML
  1. public abstract class DateUtils {
  2. private static final Logger logger = LoggerFactory.getLogger(DateUtils.class );
  3.  
  4. private static ThreadLocal<simpledateformat> defaultDateFormat = new ThreadLocal<simpledateformat>();
  5.  
  6. public static final SimpleDateFormat getDefaultDateFormat() {
  7. if (null == defaultDateFormat .get()) {
  8. defaultDateFormat .set(new SimpleDateFormat("yyyy/MM/dd" ));
  9. }
  10.  
  11. return defaultDateFormat.get();
  12. }
  13.  
  14. public static final Date pareseDate(String date) {
  15. Date result = null ;
  16. try {
  17. result = getDefaultDateFormat().parse(date);
  18. } catch (ParseException e) {
  19. logger .error( "Can't parse {} to Date", date);
  20. }
  21.  
  22. return result;
  23. }
  24.  
  25. public static final String formatDate(Date date) {
  26. return getDefaultDateFormat().format(date);
  27. }
  28. }

URL: http://mactruecolor.blogspot.com/2010/09/improve-simpledateformat-performance.html

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.