acromusashi.kafka.log.producer.util.StrftimeFormatMapper.java Source code

Java tutorial

Introduction

Here is the source code for acromusashi.kafka.log.producer.util.StrftimeFormatMapper.java

Source

/**
* Copyright (c) Acroquest Technology Co, Ltd. All Rights Reserved.
* Please read the associated COPYRIGHTS file for more details.
*
* THE SOFTWARE IS PROVIDED BY Acroquest Technolog Co., Ltd.,
* WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
* BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDER BE LIABLE FOR ANY
* CLAIM, DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
* OR DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
*/
package acromusashi.kafka.log.producer.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;

import com.google.common.collect.Maps;

/**
 * Strftime?Java?DateFormat???????
 * 
 * @author hiroki
 */
public class StrftimeFormatMapper {
    /** Apache???Strftime */
    private static final String DEFAULT_STRFTIME = "dd/MMM/yyyy:HH:mm:ss Z";

    /** Key:Strftime?Value:Strftime??DateFromat ?Map */
    private static final Map<String, String> FORMAT_MAPPING;

    /**
     * ????
     */
    private StrftimeFormatMapper() {
    }

    /**
     * ????
     */
    static {
        FORMAT_MAPPING = Maps.newHashMap();
        FORMAT_MAPPING.put("a", "EEE");
        FORMAT_MAPPING.put("A", "EEEE");
        FORMAT_MAPPING.put("b", "MMM");
        FORMAT_MAPPING.put("B", "MMMM");
        FORMAT_MAPPING.put("c", "EEE MMM d HH:mm:ss yyyy");
        FORMAT_MAPPING.put("d", "dd");
        FORMAT_MAPPING.put("D", "MM/dd/yy");
        FORMAT_MAPPING.put("e", "dd");
        FORMAT_MAPPING.put("F", "yyyy-MM-dd");
        FORMAT_MAPPING.put("g", "yy");
        FORMAT_MAPPING.put("G", "yyyy");
        FORMAT_MAPPING.put("H", "HH");
        FORMAT_MAPPING.put("h", "MMM");
        FORMAT_MAPPING.put("I", "hh");
        FORMAT_MAPPING.put("j", "DDD");
        FORMAT_MAPPING.put("k", "HH");
        FORMAT_MAPPING.put("l", "hh");
        FORMAT_MAPPING.put("m", "MM");
        FORMAT_MAPPING.put("M", "mm");
        FORMAT_MAPPING.put("n", "\n");
        FORMAT_MAPPING.put("p", "a");
        FORMAT_MAPPING.put("P", "a");
        FORMAT_MAPPING.put("r", "hh:mm:ss a");
        FORMAT_MAPPING.put("R", "HH:mm");
        FORMAT_MAPPING.put("S", "ss");
        FORMAT_MAPPING.put("t", "\t");
        FORMAT_MAPPING.put("T", "HH:mm:ss");
        FORMAT_MAPPING.put("V", "ww");
        FORMAT_MAPPING.put("X", "HH:mm:ss");
        FORMAT_MAPPING.put("x", "MM/dd/yy");
        FORMAT_MAPPING.put("y", "yy");
        FORMAT_MAPPING.put("Y", "yyyy");
        FORMAT_MAPPING.put("Z", "z");
        FORMAT_MAPPING.put("z", "Z");
        FORMAT_MAPPING.put("%", "%");
    }

    /**
     * ??STRFTime??STRFTime??????
     * 
     * @param timeStr STRFTime??
     * @param strfFormatStr STRFTime
     * @param outputFormatStr 
     * @return ??
     * @throws ParseException 
     */
    public static String convertStftToDateStr(String timeStr, String strfFormatStr, String outputFormatStr)
            throws ParseException {
        DateFormat strfFormat;

        // STRFTime????????Apache??
        if (StringUtils.isEmpty(strfFormatStr) == true) {
            strfFormat = new SimpleDateFormat(DEFAULT_STRFTIME, Locale.ENGLISH);

        } else {
            // STRFTime?DateFormat???
            strfFormat = StrftimeFormatMapper.convertStftToDateFormat(strfFormatStr);
        }

        // STRFTime???DateFormat??Date???
        Date logDate = strfFormat.parse(timeStr);

        // ?????
        DateFormat outputDateFormat = new SimpleDateFormat(outputFormatStr);
        String returnDate = outputDateFormat.format(logDate);
        return returnDate;
    }

    /**
     * StftTime??????DateFormat???
     * 
     * @param strfFormat ??StftTime?
     * @return ???DateFormat
     */
    public static DateFormat convertStftToDateFormat(String strfFormat) {
        String convertedFormat = convertStrfToDateFormatStr(strfFormat);
        DateFormat dateFormat = new SimpleDateFormat(convertedFormat, Locale.ENGLISH);

        return dateFormat;
    }

    /**
     * StrfTime?Java?DateFormat???????
     * 
     * @param strfFormat StrfTime?
     * @return Java?DateFormat????
     */
    protected static String convertStrfToDateFormatStr(String strfFormat) {
        boolean inside = false;
        boolean mark = false;
        boolean modifiedCommand = false;

        StringBuilder builder = new StringBuilder();

        for (int i = 0; i < strfFormat.length(); i++) {
            char c = strfFormat.charAt(i);

            if (c == '%' && !mark) {
                mark = true;
            } else {
                if (mark) {
                    if (modifiedCommand) {
                        modifiedCommand = false;
                        mark = false;
                    } else {
                        inside = translateCommand(builder, strfFormat, i, inside);
                        if (c == 'O' || c == 'E') {
                            modifiedCommand = true;
                        } else {
                            mark = false;
                        }
                    }
                } else {
                    if (!inside && c != ' ') {
                        builder.append("'");
                        inside = true;
                    }

                    builder.append(c);
                }
            }
        }

        if (builder.length() > 0) {
            char lastChar = builder.charAt(builder.length() - 1);

            if (lastChar != '\'' && inside) {
                builder.append('\'');
            }
        }
        return builder.toString();
    }

    /**
     * ?????
     * 
     * @param str ?
     * @param insideQuotes ??????boolean
     * @return retVal ?????????????
     */
    protected static String quote(String str, boolean insideQuotes) {
        String retVal = str;
        if (!insideQuotes) {
            retVal = '\'' + retVal + '\'';
        }
        return retVal;
    }

    /**
     * StrfTime?Java?DateFormat????
     *
     * @param oldInside ????????????
     * @param index 
     * @param pattern ?
     * @param builder ??Java
     * @return Java???
     */
    protected static boolean translateCommand(StringBuilder builder, String pattern, int index, boolean oldInside) {
        char firstChar = pattern.charAt(index);
        boolean newInside = oldInside;

        if (firstChar == 'O' || firstChar == 'E') {
            if (index + 1 < pattern.length()) {
                newInside = translateCommand(builder, pattern, index + 1, oldInside);
            } else {
                builder.append(quote("%" + firstChar, oldInside));
            }
        } else {
            String command = FORMAT_MAPPING.get(String.valueOf(firstChar));

            if (command == null) {
                builder.append(quote("%" + firstChar, oldInside));
            } else {
                if (oldInside) {
                    builder.append('\'');
                }
                builder.append(command);
                newInside = false;
            }
        }
        return newInside;
    }
}