Search in sources :

Example 26 with DateFormat

use of java.text.DateFormat in project actor-platform by actorapp.

the class DateFormatting method getTimeFormatter.

private static java.text.DateFormat getTimeFormatter() {
    DateFormat dateFormat = TIME_FORMATTER.get();
    if (dateFormat == null) {
        dateFormat = android.text.format.DateFormat.getTimeFormat(AndroidContext.getContext());
        TIME_FORMATTER.set(dateFormat);
    }
    return dateFormat;
}
Also used : SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat)

Example 27 with DateFormat

use of java.text.DateFormat in project DataX by alibaba.

the class UnstructuredStorageReaderUtil method transportOneRecord.

public static Record transportOneRecord(RecordSender recordSender, List<ColumnEntry> columnConfigs, String[] sourceLine, String nullFormat, TaskPluginCollector taskPluginCollector) {
    Record record = recordSender.createRecord();
    Column columnGenerated = null;
    // 创建都为String类型column的record
    if (null == columnConfigs || columnConfigs.size() == 0) {
        for (String columnValue : sourceLine) {
            // not equalsIgnoreCase, it's all ok if nullFormat is null
            if (columnValue.equals(nullFormat)) {
                columnGenerated = new StringColumn(null);
            } else {
                columnGenerated = new StringColumn(columnValue);
            }
            record.addColumn(columnGenerated);
        }
        recordSender.sendToWriter(record);
    } else {
        try {
            for (ColumnEntry columnConfig : columnConfigs) {
                String columnType = columnConfig.getType();
                Integer columnIndex = columnConfig.getIndex();
                String columnConst = columnConfig.getValue();
                String columnValue = null;
                if (null == columnIndex && null == columnConst) {
                    throw DataXException.asDataXException(UnstructuredStorageReaderErrorCode.NO_INDEX_VALUE, "由于您配置了type, 则至少需要配置 index 或 value");
                }
                if (null != columnIndex && null != columnConst) {
                    throw DataXException.asDataXException(UnstructuredStorageReaderErrorCode.MIXED_INDEX_VALUE, "您混合配置了index, value, 每一列同时仅能选择其中一种");
                }
                if (null != columnIndex) {
                    if (columnIndex >= sourceLine.length) {
                        String message = String.format("您尝试读取的列越界,源文件该行有 [%s] 列,您尝试读取第 [%s] 列, 数据详情[%s]", sourceLine.length, columnIndex + 1, StringUtils.join(sourceLine, ","));
                        LOG.warn(message);
                        throw new IndexOutOfBoundsException(message);
                    }
                    columnValue = sourceLine[columnIndex];
                } else {
                    columnValue = columnConst;
                }
                Type type = Type.valueOf(columnType.toUpperCase());
                // it's all ok if nullFormat is null
                if (columnValue.equals(nullFormat)) {
                    columnValue = null;
                }
                switch(type) {
                    case STRING:
                        columnGenerated = new StringColumn(columnValue);
                        break;
                    case LONG:
                        try {
                            columnGenerated = new LongColumn(columnValue);
                        } catch (Exception e) {
                            throw new IllegalArgumentException(String.format("类型转换错误, 无法将[%s] 转换为[%s]", columnValue, "LONG"));
                        }
                        break;
                    case DOUBLE:
                        try {
                            columnGenerated = new DoubleColumn(columnValue);
                        } catch (Exception e) {
                            throw new IllegalArgumentException(String.format("类型转换错误, 无法将[%s] 转换为[%s]", columnValue, "DOUBLE"));
                        }
                        break;
                    case BOOLEAN:
                        try {
                            columnGenerated = new BoolColumn(columnValue);
                        } catch (Exception e) {
                            throw new IllegalArgumentException(String.format("类型转换错误, 无法将[%s] 转换为[%s]", columnValue, "BOOLEAN"));
                        }
                        break;
                    case DATE:
                        try {
                            if (columnValue == null) {
                                Date date = null;
                                columnGenerated = new DateColumn(date);
                            } else {
                                String formatString = columnConfig.getFormat();
                                //if (null != formatString) {
                                if (StringUtils.isNotBlank(formatString)) {
                                    // 用户自己配置的格式转换, 脏数据行为出现变化
                                    DateFormat format = columnConfig.getDateFormat();
                                    columnGenerated = new DateColumn(format.parse(columnValue));
                                } else {
                                    // 框架尝试转换
                                    columnGenerated = new DateColumn(new StringColumn(columnValue).asDate());
                                }
                            }
                        } catch (Exception e) {
                            throw new IllegalArgumentException(String.format("类型转换错误, 无法将[%s] 转换为[%s]", columnValue, "DATE"));
                        }
                        break;
                    default:
                        String errorMessage = String.format("您配置的列类型暂不支持 : [%s]", columnType);
                        LOG.error(errorMessage);
                        throw DataXException.asDataXException(UnstructuredStorageReaderErrorCode.NOT_SUPPORT_TYPE, errorMessage);
                }
                record.addColumn(columnGenerated);
            }
            recordSender.sendToWriter(record);
        } catch (IllegalArgumentException iae) {
            taskPluginCollector.collectDirtyRecord(record, iae.getMessage());
        } catch (IndexOutOfBoundsException ioe) {
            taskPluginCollector.collectDirtyRecord(record, ioe.getMessage());
        } catch (Exception e) {
            if (e instanceof DataXException) {
                throw (DataXException) e;
            }
            // 每一种转换失败都是脏数据处理,包括数字格式 & 日期格式
            taskPluginCollector.collectDirtyRecord(record, e.getMessage());
        }
    }
    return record;
}
Also used : DataXException(com.alibaba.datax.common.exception.DataXException) UnsupportedCharsetException(java.nio.charset.UnsupportedCharsetException) Date(java.util.Date) DateFormat(java.text.DateFormat) DataXException(com.alibaba.datax.common.exception.DataXException)

Example 28 with DateFormat

use of java.text.DateFormat in project druid by alibaba.

the class DruidStatService method getSqlStat.

private String getSqlStat(Integer id) {
    Map<String, Object> map = statManagerFacade.getSqlStatData(id);
    if (map == null) {
        return returnJSONResult(RESULT_CODE_ERROR, null);
    }
    String dbType = (String) map.get("DbType");
    String sql = (String) map.get("SQL");
    map.put("formattedSql", SQLUtils.format(sql, dbType));
    List<SQLStatement> statementList = SQLUtils.parseStatements(sql, dbType);
    if (!statementList.isEmpty()) {
        SQLStatement sqlStmt = statementList.get(0);
        SchemaStatVisitor visitor = SQLUtils.createSchemaStatVisitor(dbType);
        sqlStmt.accept(visitor);
        map.put("parsedTable", visitor.getTables().toString());
        map.put("parsedFields", visitor.getColumns().toString());
        map.put("parsedConditions", visitor.getConditions().toString());
        map.put("parsedRelationships", visitor.getRelationships().toString());
        map.put("parsedOrderbycolumns", visitor.getOrderByColumns().toString());
    }
    DateFormat format = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss:SSS");
    Date maxTimespanOccurTime = (Date) map.get("MaxTimespanOccurTime");
    if (maxTimespanOccurTime != null) {
        map.put("MaxTimespanOccurTime", format.format(maxTimespanOccurTime));
    }
    return returnJSONResult(map == null ? RESULT_CODE_ERROR : RESULT_CODE_SUCCESS, map);
}
Also used : SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) SimpleDateFormat(java.text.SimpleDateFormat) SchemaStatVisitor(com.alibaba.druid.sql.visitor.SchemaStatVisitor) Date(java.util.Date)

Example 29 with DateFormat

use of java.text.DateFormat in project fastjson by alibaba.

the class JSONSerializer method writeWithFormat.

public final void writeWithFormat(Object object, String format) {
    if (object instanceof Date) {
        DateFormat dateFormat = this.getDateFormat();
        if (dateFormat == null) {
            dateFormat = new SimpleDateFormat(format, locale);
            dateFormat.setTimeZone(timeZone);
        }
        String text = dateFormat.format((Date) object);
        out.writeString(text);
        return;
    }
    write(object);
}
Also used : SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date)

Example 30 with DateFormat

use of java.text.DateFormat in project hudson-2.x by hudson.

the class CronTabTest method compare.

/**
     * Humans can't easily see difference in two {@link Calendar}s, do help the diagnosis by using {@link DateFormat}. 
     */
private void compare(Calendar a, Calendar b) {
    DateFormat f = DateFormat.getDateTimeInstance();
    System.out.println(f.format(a.getTime()) + " vs " + f.format(b.getTime()));
    assertEquals(a, b);
}
Also used : DateFormat(java.text.DateFormat)

Aggregations

DateFormat (java.text.DateFormat)646 SimpleDateFormat (java.text.SimpleDateFormat)486 Date (java.util.Date)315 ParseException (java.text.ParseException)132 Calendar (java.util.Calendar)78 Test (org.junit.Test)69 ArrayList (java.util.ArrayList)48 IOException (java.io.IOException)43 File (java.io.File)31 HashMap (java.util.HashMap)27 GregorianCalendar (java.util.GregorianCalendar)24 TimeZone (java.util.TimeZone)23 Locale (java.util.Locale)18 Timestamp (java.sql.Timestamp)17 List (java.util.List)14 InputStream (java.io.InputStream)12 DateTime (org.joda.time.DateTime)11 Map (java.util.Map)10 Matcher (java.util.regex.Matcher)8 TestBean (org.springframework.tests.sample.beans.TestBean)8