001/** 002 * 003 * Copyright (c) 2014, the Railo Company Ltd. All rights reserved. 004 * 005 * This library is free software; you can redistribute it and/or 006 * modify it under the terms of the GNU Lesser General Public 007 * License as published by the Free Software Foundation; either 008 * version 2.1 of the License, or (at your option) any later version. 009 * 010 * This library is distributed in the hope that it will be useful, 011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 013 * Lesser General Public License for more details. 014 * 015 * You should have received a copy of the GNU Lesser General Public 016 * License along with this library. If not, see <http://www.gnu.org/licenses/>. 017 * 018 **/ 019package lucee.commons.i18n; 020 021import java.text.SimpleDateFormat; 022import java.util.Date; 023import java.util.Locale; 024import java.util.Map; 025import java.util.TimeZone; 026import java.util.WeakHashMap; 027 028/** 029 * 030 */ 031public final class DateFormatPool { 032 033 private final static Map data=new WeakHashMap(); 034 035 /** 036 * pool for formated dates 037 * @param locale 038 * @param timeZone 039 * @param pattern 040 * @param date 041 * @return date matching given values 042 */ 043 public static synchronized String format(Locale locale, TimeZone timeZone, String pattern,Date date) { 044 String key=locale.toString()+'-'+timeZone.getID()+'-'+pattern; 045 Object obj=data.get(key); 046 if(obj!=null) { 047 return ((SimpleDateFormat)obj).format(date); 048 } 049 SimpleDateFormat sdf = new SimpleDateFormat(pattern,locale); 050 sdf.setTimeZone(timeZone); 051 data.put(key,sdf); 052 return sdf.format(date); 053 } 054 055 /** 056 * pool for formated dates 057 * @param locale 058 * @param pattern 059 * @param date 060 * @return date matching given values 061 */ 062 public static synchronized String format(Locale locale, String pattern,Date date) { 063 String key=locale.toString()+'-'+pattern; 064 065 Object obj=data.get(key); 066 if(obj!=null) { 067 return ((SimpleDateFormat)obj).format(date); 068 }//print.ln(key); 069 SimpleDateFormat sdf = new SimpleDateFormat(pattern,locale); 070 data.put(key,sdf); 071 return sdf.format(date); 072 } 073 074 /** 075 * pool for formated dates 076 * @param pattern 077 * @param date 078 * @return date matching given values 079 */ 080 public static synchronized String format(String pattern,Date date) { 081 Object obj=data.get(pattern); 082 if(obj!=null) { 083 return ((SimpleDateFormat)obj).format(date); 084 }//print.ln(pattern); 085 SimpleDateFormat sdf = new SimpleDateFormat(pattern); 086 data.put(pattern,sdf); 087 return sdf.format(date); 088 } 089 090}