Search in sources :

Example 11 with Format

use of java.text.Format in project phoenix by apache.

the class ResultUtil method getSuffix.

public String getSuffix() {
    if (null == FILE_SUFFIX) {
        Date date = new Date();
        Format formatter = new SimpleDateFormat("YYYY-MM-dd_hh-mm-ss");
        FILE_SUFFIX = formatter.format(date);
    }
    return "_" + FILE_SUFFIX;
}
Also used : Format(java.text.Format) SimpleDateFormat(java.text.SimpleDateFormat) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date)

Example 12 with Format

use of java.text.Format in project phoenix by apache.

the class ToCharParseNode method create.

@Override
public FunctionExpression create(List<Expression> children, StatementContext context) throws SQLException {
    PDataType dataType = children.get(0).getDataType();
    // either date or number format string
    String formatString = (String) ((LiteralExpression) children.get(1)).getValue();
    Format formatter;
    FunctionArgumentType type;
    if (dataType.isCoercibleTo(PTimestamp.INSTANCE)) {
        if (formatString == null) {
            formatString = context.getDateFormat();
            formatter = context.getDateFormatter();
        } else {
            formatter = FunctionArgumentType.TEMPORAL.getFormatter(formatString);
        }
        type = FunctionArgumentType.TEMPORAL;
    } else if (dataType.isCoercibleTo(PDecimal.INSTANCE)) {
        if (formatString == null)
            formatString = context.getNumberFormat();
        formatter = FunctionArgumentType.NUMERIC.getFormatter(formatString);
        type = FunctionArgumentType.NUMERIC;
    } else {
        throw new SQLException(dataType + " type is unsupported for TO_CHAR().  Numeric and temporal types are supported.");
    }
    return new ToCharFunction(children, type, formatString, formatter);
}
Also used : Format(java.text.Format) PDataType(org.apache.phoenix.schema.types.PDataType) SQLException(java.sql.SQLException) FunctionArgumentType(org.apache.phoenix.expression.function.FunctionArgumentType) ToCharFunction(org.apache.phoenix.expression.function.ToCharFunction)

Example 13 with Format

use of java.text.Format in project poi by apache.

the class DataFormatter method getFormattedDateString.

/**
     * Returns the formatted value of an Excel date as a <tt>String</tt> based
     * on the cell's <code>DataFormat</code>. i.e. "Thursday, January 02, 2003"
     * , "01/02/2003" , "02-Jan" , etc.
     * <p/>
     * If any conditional format rules apply, the highest priority with a number format is used.
     * If no rules contain a number format, or no rules apply, the cell's style format is used.
     * If the style does not have a format, the default date format is applied.
     *
     * @param cell to format
     * @param cfEvaluator ConditionalFormattingEvaluator (if available)
     * @return Formatted value
     */
private String getFormattedDateString(Cell cell, ConditionalFormattingEvaluator cfEvaluator) {
    Format dateFormat = getFormat(cell, cfEvaluator);
    if (dateFormat instanceof ExcelStyleDateFormatter) {
        // Hint about the raw excel value
        ((ExcelStyleDateFormatter) dateFormat).setDateToBeFormatted(cell.getNumericCellValue());
    }
    Date d = cell.getDateCellValue();
    return performDateFormatting(d, dateFormat);
}
Also used : Format(java.text.Format) SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) DecimalFormat(java.text.DecimalFormat) CellFormat(org.apache.poi.ss.format.CellFormat) Date(java.util.Date)

Example 14 with Format

use of java.text.Format in project poi by apache.

the class DataFormatter method formatRawCellContents.

/**
     * Formats the given raw cell value, based on the supplied
     *  format index and string, according to excel style rules.
     * @see #formatCellValue(Cell)
     */
public String formatRawCellContents(double value, int formatIndex, String formatString, boolean use1904Windowing) {
    localeChangedObservable.checkForLocaleChange();
    // Is it a date?
    if (DateUtil.isADateFormat(formatIndex, formatString)) {
        if (DateUtil.isValidExcelDate(value)) {
            Format dateFormat = getFormat(value, formatIndex, formatString);
            if (dateFormat instanceof ExcelStyleDateFormatter) {
                // Hint about the raw excel value
                ((ExcelStyleDateFormatter) dateFormat).setDateToBeFormatted(value);
            }
            Date d = DateUtil.getJavaDate(value, use1904Windowing);
            return performDateFormatting(d, dateFormat);
        }
        // RK: Invalid dates are 255 #s.
        if (emulateCSV) {
            return invalidDateTimeString;
        }
    }
    // else Number
    Format numberFormat = getFormat(value, formatIndex, formatString);
    if (numberFormat == null) {
        return String.valueOf(value);
    }
    // When formatting 'value', double to text to BigDecimal produces more
    // accurate results than double to Double in JDK8 (as compared to
    // previous versions). However, if the value contains E notation, this
    // would expand the values, which we do not want, so revert to
    // original method.
    String result;
    final String textValue = NumberToTextConverter.toText(value);
    if (textValue.indexOf('E') > -1) {
        result = numberFormat.format(new Double(value));
    } else {
        result = numberFormat.format(new BigDecimal(textValue));
    }
    // Complete scientific notation by adding the missing +.
    if (result.indexOf('E') > -1 && !result.contains("E-")) {
        result = result.replaceFirst("E", "E+");
    }
    return result;
}
Also used : Format(java.text.Format) SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) DecimalFormat(java.text.DecimalFormat) CellFormat(org.apache.poi.ss.format.CellFormat) Date(java.util.Date) BigDecimal(java.math.BigDecimal)

Example 15 with Format

use of java.text.Format in project poi by apache.

the class DataFormatter method getFormat.

private Format getFormat(double cellValue, int formatIndex, String formatStrIn) {
    localeChangedObservable.checkForLocaleChange();
    //      // Might be better to separate out the n p and z formats, falling back to p when n and z are not set.
    //      // That however would require other code to be re factored.
    //      String[] formatBits = formatStrIn.split(";");
    //      int i = cellValue > 0.0 ? 0 : cellValue < 0.0 ? 1 : 2; 
    //      String formatStr = (i < formatBits.length) ? formatBits[i] : formatBits[0];
    String formatStr = formatStrIn;
    // TODO Going forward, we should really merge the logic between the two classes
    if (formatStr.contains(";") && (formatStr.indexOf(';') != formatStr.lastIndexOf(';') || rangeConditionalPattern.matcher(formatStr).matches())) {
        try {
            // Ask CellFormat to get a formatter for it
            CellFormat cfmt = CellFormat.getInstance(formatStr);
            // CellFormat requires callers to identify date vs not, so do so
            Object cellValueO = Double.valueOf(cellValue);
            if (DateUtil.isADateFormat(formatIndex, formatStr) && // don't try to handle Date value 0, let a 3 or 4-part format take care of it 
            ((Double) cellValueO).doubleValue() != 0.0) {
                cellValueO = DateUtil.getJavaDate(cellValue);
            }
            // Wrap and return (non-cachable - CellFormat does that)
            return new CellFormatResultWrapper(cfmt.apply(cellValueO));
        } catch (Exception e) {
            logger.log(POILogger.WARN, "Formatting failed for format " + formatStr + ", falling back", e);
        }
    }
    // Excel's # with value 0 will output empty where Java will output 0. This hack removes the # from the format.
    if (emulateCSV && cellValue == 0.0 && formatStr.contains("#") && !formatStr.contains("0")) {
        formatStr = formatStr.replaceAll("#", "");
    }
    // See if we already have it cached
    Format format = formats.get(formatStr);
    if (format != null) {
        return format;
    }
    // Is it one of the special built in types, General or @?
    if ("General".equalsIgnoreCase(formatStr) || "@".equals(formatStr)) {
        return generalNumberFormat;
    }
    // Build a formatter, and cache it
    format = createFormat(cellValue, formatIndex, formatStr);
    formats.put(formatStr, format);
    return format;
}
Also used : Format(java.text.Format) SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) DecimalFormat(java.text.DecimalFormat) CellFormat(org.apache.poi.ss.format.CellFormat) CellFormat(org.apache.poi.ss.format.CellFormat)

Aggregations

Format (java.text.Format)146 SimpleDateFormat (java.text.SimpleDateFormat)60 DecimalFormat (java.text.DecimalFormat)39 Test (org.junit.Test)38 DateFormat (java.text.DateFormat)34 NumberFormat (java.text.NumberFormat)29 DateTimeFormatter (java.time.format.DateTimeFormatter)27 Date (java.util.Date)26 ChoiceFormat (java.text.ChoiceFormat)24 ParsePosition (java.text.ParsePosition)22 MessageFormat (java.text.MessageFormat)20 FieldPosition (java.text.FieldPosition)16 Test (org.testng.annotations.Test)16 IOException (java.io.IOException)14 ParseException (java.text.ParseException)11 Locale (java.util.Locale)10 AttributedString (java.text.AttributedString)9 ArrayList (java.util.ArrayList)9 TemporalAccessor (java.time.temporal.TemporalAccessor)7 Calendar (java.util.Calendar)7