Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Licensed to the Apache Software Foundation (ASF) under one or more
   * contributor license agreements.  See the NOTICE file distributed with
   * this work for additional information regarding copyright ownership.
   * The ASF licenses this file to You under the Apache License, Version 2.0
   * (the "License"); you may not use this file except in compliance with
   * the License.  You may obtain a copy of the License at
   * 
   *      http://www.apache.org/licenses/LICENSE-2.0
  * 
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 
 package org.apache.catalina.util;
 
 import java.util.Date;
Converts dates to strings using the same format specifiers as strftime Note: This does not mimic strftime perfectly. Certain strftime commands, are not supported, and will convert as if they were literals. Certain complicated commands, like those dealing with the week of the year probably don't have exactly the same behavior as strftime. These limitations are due to use SimpleDateTime. If the conversion was done manually, all these limitations could be eliminated. The interface looks like a subset of DateFormat. Maybe someday someone will make this class extend DateFormat.

Author(s):
Bip Thelin
Dan Sandberg
Version:
$Revision: 1237 $, $Date: 2009-11-03 02:55:48 +0100 (Tue, 03 Nov 2009) $
 
 public class Strftime {
     protected static Properties translate;
     protected SimpleDateFormat simpleDateFormat;

    
Initialize our pattern translation
 
     static {
          = new Properties();
         .put("a","EEE");
         .put("A","EEEE");
         .put("b","MMM");
         .put("B","MMMM");
         .put("c","EEE MMM d HH:mm:ss yyyy");
 
         //There's no way to specify the century in SimpleDateFormat.  We don't want to hard-code
         //20 since this could be wrong for the pre-2000 files.
         //translate.put("C", "20");
         .put("d","dd");
         .put("D","MM/dd/yy");
         .put("e","dd"); //will show as '03' instead of ' 3'
         .put("F","yyyy-MM-dd");
         .put("g","yy");
         .put("G","yyyy");
         .put("H","HH");
         .put("h","MMM");
         .put("I","hh");
         .put("j","DDD");
         .put("k","HH"); //will show as '07' instead of ' 7'
         .put("l","hh"); //will show as '07' instead of ' 7'
         .put("m","MM");
         .put("M","mm");
         .put("n","\n");
         .put("p","a");
         .put("P","a");  //will show as pm instead of PM
         .put("r","hh:mm:ss a");
         .put("R","HH:mm");
         //There's no way to specify this with SimpleDateFormat
         //translate.put("s","seconds since ecpoch");
         .put("S","ss");
         .put("t","\t");
         .put("T","HH:mm:ss");
         //There's no way to specify this with SimpleDateFormat
         //translate.put("u","day of week ( 1-7 )");
 
         //There's no way to specify this with SimpleDateFormat
         //translate.put("U","week in year with first sunday as first day...");
 
         .put("V","ww"); //I'm not sure this is always exactly the same
 
         //There's no way to specify this with SimpleDateFormat
         //translate.put("W","week in year with first monday as first day...");
 
         //There's no way to specify this with SimpleDateFormat
         //translate.put("w","E");
        .put("X","HH:mm:ss");
        .put("x","MM/dd/yy");
        .put("y","yy");
        .put("Y","yyyy");
        .put("Z","z");
        .put("z","Z");
        .put("%","%");
    }


    
Create an instance of this date formatting class

    public StrftimeString origFormat ) {
        String convertedFormat = convertDateFormatorigFormat );
         = new SimpleDateFormatconvertedFormat );
    }

    
Create an instance of this date formatting class

Parameters:
origFormat the strftime-style formatting string
locale the locale to use for locale-specific conversions
    public StrftimeString origFormatLocale locale ) {
        String convertedFormat = convertDateFormatorigFormat );
         = new SimpleDateFormatconvertedFormatlocale );
    }

    
Format the date according to the strftime-style string given in the constructor.

Parameters:
date the date to format
Returns:
the formatted date
    public String formatDate date ) {
        return .formatdate );
    }

    
Get the timezone used for formatting conversions

Returns:
the timezone
    public TimeZone getTimeZone() {
        return .getTimeZone();
    }

    
Change the timezone used to format dates

    public void setTimeZoneTimeZone timeZone ) {
        .setTimeZonetimeZone );
    }

    
Search the provided pattern and get the C standard Date/Time formatting rules and convert them to the Java equivalent.

Parameters:
pattern The pattern to search
Returns:
The modified pattern
    protected String convertDateFormatString pattern ) {
        boolean inside = false;
        boolean mark = false;
        boolean modifiedCommand = false;
        StringBuilder buf = new StringBuilder();
        for(int i = 0; i < pattern.length(); i++) {
            char c = pattern.charAt(i);
            if ( c=='%' && !mark ) {
                mark=true;
            } else {
                if ( mark ) {
                    if ( modifiedCommand ) {
                        //don't do anything--we just wanted to skip a char
                        modifiedCommand = false;
                        mark = false;
                    } else {
                        inside = translateCommandbufpatterniinside );
                        //It's a modifier code
                        if ( c=='O' || c=='E' ) {
                            modifiedCommand = true;
                        } else {
                            mark=false;
                        }
                    }
                } else {
                    if ( !inside && c != ' ' ) {
                        //We start a literal, which we need to quote
                        buf.append("'");
                        inside = true;
                    }
                    
                    buf.append(c);
                }
            }
        }
        if ( buf.length() > 0 ) {
            char lastChar = buf.charAtbuf.length() - 1 );
            iflastChar!='\'' && inside ) {
                buf.append('\'');
            }
        }
        return buf.toString();
    }
    protected String quoteString strboolean insideQuotes ) {
        String retVal = str;
        if ( !insideQuotes ) {
            retVal = '\'' + retVal + '\'';
        }
        return retVal;
    }

    
Try to get the Java Date/Time formatting associated with the C standard provided.

Parameters:
buf The buffer
pattern The date/time pattern
index The char index
oldInside Flag value
Returns:
True if new is inside buffer
    protected boolean translateCommandStringBuilder bufString patternint indexboolean oldInside ) {
        char firstChar = pattern.charAtindex );
        boolean newInside = oldInside;
        //O and E are modifiers, they mean to present an alternative representation of the next char
        //we just handle the next char as if the O or E wasn't there
        if ( firstChar == 'O' || firstChar == 'E' ) {
            if ( index + 1 < pattern.length() ) {               
                newInside = translateCommandbufpatternindex + 1, oldInside );
            } else {
                buf.appendquote("%" + firstCharoldInside ) );
            }
        } else {
            String command = .getProperty( String.valueOffirstChar ) );
            
            //If we don't find a format, treat it as a literal--That's what apache does
            if ( command == null ) {
                buf.appendquote"%" + firstCharoldInside ) );
            } else {
                //If we were inside quotes, close the quotes
                if ( oldInside ) {
                    buf.append'\'' );
                }
                buf.appendcommand );
                newInside = false;
            }
        }
        return newInside;
    }
New to GrepCode? Check out our FAQ X