FileDocCategorySizeDatePackage
DateFormater.javaAPI DocApache Lucene 2.1.04733Wed Feb 14 10:46:04 GMT 2007org.apache.lucene.gdata.utils

DateFormater

public class DateFormater extends Object
This class uses the {@link java.text.SimpleDateFormat} class to format dates into strings according to given date pattern.

As the creation of SimpleDateFormat objects is quiet expensive and formating dates is used quiet fequently the objects will be cached and reused in subsequent calls.

This implementation is thread safe as it uses {@link java.util.Stack} as a cache

author
Simon Willnauer

Fields Summary
private final Stack
objectStack
private static final DateFormater
formater
public static final String
HTTP_HEADER_DATE_FORMAT
Date format as it is used in Http Last modified header (Tue, 15 Nov 1994 12:45:26 GMT)
public static final String
HTTP_HEADER_DATE_FORMAT_TIME_OFFSET
Date format as it is used in Http Last modified header (Tue, 15 Nov 1994 12:45:26 +0000)
Constructors Summary
protected DateFormater()


      
        super();
    
Methods Summary
public static java.lang.StringformatDate(java.util.Date date, java.lang.String format)
Formats the given Date into the given date pattern.

param
date - the date to format
param
format - date pattern
return
- the string representation of the given Date according to the given pattern

        if (date == null || format == null)
            throw new IllegalArgumentException(
                    "given parameters must not be null");
        SimpleDateFormat inst = formater.getFormater();
        inst.applyPattern(format);
        try{
            return inst.format(date);
        }finally{
            formater.returnFomater(inst);
        }
    
protected java.text.SimpleDateFormatgetFormater()

        if (this.objectStack.empty())
            return new SimpleDateFormat(DateFormater.HTTP_HEADER_DATE_FORMAT,Locale.ENGLISH);
        return this.objectStack.pop();
    
    
public static java.util.DateparseDate(java.lang.String date, java.lang.String formates)
Parses the given string into one of the specified formates

param
date - the string to parse
param
formates - formates
return
a {@link Date} instance representing the given string
throws
ParseException - if the string can not be parsed

        for (int i = 0; i < formates.length; i++) {
            try {
             return parseDate(date,formates[i]);
            } catch (ParseException e) {
                //
            }
        }
        throw new ParseException("Unparseable date: "+date,0);
        
    
public static java.util.DateparseDate(java.lang.String dateString, java.lang.String pattern)
Parses the given string into the specified formate

param
dateString - the string to parse
param
pattern - the expected formate
return
a {@link Date} instance representing the given string
throws
ParseException - if the string can not be parsed

        if(dateString == null|| pattern == null)
            throw new IllegalArgumentException(
            "given parameters must not be null");
        
        SimpleDateFormat inst = formater.getFormater();
        try{
        inst.applyPattern(pattern);
        return inst.parse(dateString);
        }finally{
            formater.returnFomater(inst);
        }
        
    
protected voidreturnFomater(java.text.SimpleDateFormat format)

        if (this.objectStack.size() <= 25)
            this.objectStack.push(format);