Example usage for org.joda.time.format DateTimeFormatter parseMillis

List of usage examples for org.joda.time.format DateTimeFormatter parseMillis

Introduction

In this page you can find the example usage for org.joda.time.format DateTimeFormatter parseMillis.

Prototype

public long parseMillis(String text) 

Source Link

Document

Parses a datetime from the given text, returning the number of milliseconds since the epoch, 1970-01-01T00:00:00Z.

Usage

From source file:bison.solutions.hazelcast.HazelcastConnection.java

private void putCitationsInHazelcast(InputStream citation) {

    try (InputStreamReader inputStreamReader = new InputStreamReader(citation);
            BufferedReader bufferedReader = new BufferedReader(inputStreamReader)) {

        String line;/*from   w  ww  .  ja va 2  s .c o  m*/
        bufferedReader.readLine();
        Citation citationToPutInHaz = null;
        String key = "";
        while ((line = bufferedReader.readLine()) != null) {
            try {
                String[] values = line.split(",");
                citationToPutInHaz = new Citation();
                DateTimeFormatter dtf = DateTimeFormat.forPattern("MM/dd/yyyy");

                key = values[0];
                citationToPutInHaz.setCitationNumber(Integer.parseInt(values[1]));
                citationToPutInHaz.setFirstName(values[3]);
                citationToPutInHaz.setLastName(values[4]);
                citationToPutInHaz.setDefendentAddress(values[6]);
                citationToPutInHaz.setDefendentCity(values[7]);
                citationToPutInHaz.setDefendentState(values[8]);
                citationToPutInHaz.setDriversLicenseNumber(values[9]);
                citationToPutInHaz.setCourtLocation(values[11]);
                citationToPutInHaz.setCourtAddress(values[12]);

                try {
                    citationToPutInHaz.setCitationDate(new Date(dtf.parseMillis(values[2].split(" ")[0])));
                } catch (IllegalArgumentException e) {
                    /*literally cancer*/}
                try {
                    citationToPutInHaz.setDateOfBirth(new Date(dtf.parseMillis(values[5].split(" ")[0])));
                } catch (IllegalArgumentException e) {
                    /*literally cancer*/}
                try {
                    citationToPutInHaz.setCourtDate(new Date(dtf.parseMillis(values[10].split(" ")[0])));
                } catch (IllegalArgumentException e) {
                    /*literally cancer*/}

                hazelcastInstance.getMap(CitationNamespace).put(key, citationToPutInHaz);
            } catch (ArrayIndexOutOfBoundsException e) {
                hazelcastInstance.getMap(CitationNamespace).put(key, citationToPutInHaz);
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:bison.solutions.hazelcast.HazelcastConnection.java

private void putViolationsInHazelcast(InputStream violation) {
    try (InputStreamReader inputStreamReader = new InputStreamReader(violation);
            BufferedReader bufferedReader = new BufferedReader(inputStreamReader)) {

        String line;/*  w w  w. j a v a2 s. co  m*/
        bufferedReader.readLine();
        while ((line = bufferedReader.readLine()) != null) {
            String[] values = line.split(",");
            Violation violationToPutInHaz = new Violation();
            DateTimeFormatter dtf = DateTimeFormat.forPattern("MM/dd/yyyy");

            String key = values[0];
            try {
                violationToPutInHaz.setCitationNumber(Long.parseLong(values[1]));
                violationToPutInHaz.setViolationNumber(values[2]);
                violationToPutInHaz.setViolationDescription(values[3]);
                violationToPutInHaz.setWarrantStatus(parseTOrF(values[4]));
                violationToPutInHaz.setWarrantNumber(values[5]);
                try {
                    violationToPutInHaz.setStatus(Violation.Status.valueOf(values[6]));
                } catch (IllegalArgumentException ex) {
                    /* */ }
                violationToPutInHaz.setStatusDate(new Date(dtf.parseMillis(values[7])));
                violationToPutInHaz.setFineAmount(values[8]);
                violationToPutInHaz.setCourtCost(values[9]);
            } catch (ArrayIndexOutOfBoundsException e) {
            }

            hazelcastInstance.getMap(ViolationNamespace).put(key, violationToPutInHaz);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:com.cohort.util.Calendar2.java

License:Open Source License

/**
 * This converts s into a double with epochSeconds.
 *
 * @param dateTimeFormat one of the ISO8601 formats above, or a Joda format.
 *   If it starts with "yyyy-MM", sourceTime will be parsed with Calendar2.parseISODateTimeZulu();
 *   else parse with Joda. /*  w  w  w .  j a  va  2s.co m*/
 * @return the epochSeconds value or NaN if trouble
 */
public static double toEpochSeconds(String sourceTime, String dateTimeFormat) {
    try {
        if (dateTimeFormat.startsWith("yyyy-MM"))
            //parse with Calendar2.parseISODateTime
            return safeIsoStringToEpochSeconds(sourceTime);

        //parse with Joda
        DateTimeFormatter formatter = DateTimeFormat.forPattern(dateTimeFormat).withZone(DateTimeZone.UTC);
        return formatter.parseMillis(sourceTime) / 1000.0; //thread safe

    } catch (Throwable t) {
        if (verbose && sourceTime != null && sourceTime.length() > 0)
            String2.log("  EDVTimeStamp.sourceTimeToEpochSeconds: Invalid sourceTime=" + sourceTime + " format="
                    + dateTimeFormat + "\n" + t.toString());
        return Double.NaN;
    }
}

From source file:com.cohort.util.Calendar2.java

License:Open Source License

/**
 * This converts sa into a DoubleArray with epochSeconds.
 *
 * @param dateTimeFormat one of the ISO8601 formats above, or a Joda format.
 *   If it starts with "yyyy-MM", sa strings will be parsed with Calendar2.parseISODateTimeZulu();
 *   else parse with Joda. /*from  ww  w  .j a  v  a  2s  .c o m*/
 * @return a DoubleArray with the epochSeconds values (any/all will be NaN if touble)
 */
public static DoubleArray toEpochSeconds(StringArray sa, String dateTimeFormat) {
    int n = sa.size();
    DoubleArray da = new DoubleArray(n, false);
    if (dateTimeFormat == null || dateTimeFormat.length() == 0) {
        da.addN(n, Double.NaN);
        return da;
    }
    try {

        if (dateTimeFormat.startsWith("yyyy-MM")) {
            //use Calendar2
            for (int i = 0; i < n; i++)
                da.add(safeIsoStringToEpochSeconds(sa.get(i)));
        } else {
            //use Joda
            boolean printError = verbose;
            DateTimeFormatter formatter = DateTimeFormat.forPattern(dateTimeFormat).withZone(DateTimeZone.UTC);
            da.addN(n, Double.NaN);
            for (int i = 0; i < n; i++) {
                String s = sa.get(i);
                if (s != null && s.length() > 0) {
                    try {
                        da.set(i, formatter.parseMillis(s) / 1000.0); //thread safe
                    } catch (Throwable t2) {
                        if (printError) {
                            String2.log(
                                    "  EDVTimeStamp.sourceTimeToEpochSeconds: error while parsing sourceTime="
                                            + s + " with format=" + dateTimeFormat + "\n" + t2.toString());
                            printError = false;
                        }
                    }
                }
            }
        }

    } catch (Throwable t) {
        if (verbose)
            String2.log("  Calendar2.toEpochSeconds: format=" + dateTimeFormat + ", Unexpected error="
                    + t.toString());
    }
    return da;

}

From source file:com.esri.geoevent.clusterSimulator.simulator.DateTimeParser.java

License:Apache License

public static long parseTime(String timeString) {
    if (timeString == null)
        return 0;
    try {/*from w  w  w .j  a va2 s  . c  o  m*/
        DateTimeFormatter formatter = ISODateTimeFormat.dateTimeParser().withZoneUTC();
        return formatter.parseMillis(timeString);
    } catch (IllegalArgumentException e) {
    }

    try {
        return DateFormat.getDateInstance().parse(timeString).getTime();
    } catch (ParseException | NumberFormatException e) {
    }

    try {
        DateTimeFormatter formatter = DateTimeFormat.forPattern("MM/dd/yy hh:mm:ss aa");
        return formatter.parseMillis(timeString);
    } catch (IllegalArgumentException e) {
    }

    try {
        DateTimeFormatter formatter = DateTimeFormat.forPattern("MM/dd/yy HH:mm:ss");
        return formatter.parseMillis(timeString);
    } catch (IllegalArgumentException e) {
    }

    return 0;
}

From source file:com.facebook.presto.operator.scalar.DateTimeFunctions.java

License:Apache License

@ScalarFunction
@SqlType(TimestampType.class)
public static long dateParse(ConnectorSession session, @SqlType(VarcharType.class) Slice dateTime,
        @SqlType(VarcharType.class) Slice formatString) {
    DateTimeFormatter formatter = DATETIME_FORMATTER_CACHE.get(formatString)
            .withChronology(getChronology(session.getTimeZoneKey())).withLocale(session.getLocale());

    try {/*from   w  w  w.ja  v a 2 s. c  o m*/
        return formatter.parseMillis(dateTime.toString(Charsets.UTF_8));
    } catch (IllegalArgumentException e) {
        throw new PrestoException(StandardErrorCode.INVALID_FUNCTION_ARGUMENT.toErrorCode(), e);
    }
}

From source file:com.facebook.presto.operator.scalar.UnixTimeFunctions.java

License:Apache License

@Description("parses the specified date/time by the given format")
@ScalarFunction/*ww  w  .  j a v a 2 s . c o m*/
public static long parseDatetime(Slice datetime, Slice formatString) {
    String pattern = formatString.toString(Charsets.UTF_8);
    DateTimeFormatter formatter = DateTimeFormat.forPattern(pattern).withZoneUTC();

    String datetimeString = datetime.toString(Charsets.UTF_8);
    return fromMillis(formatter.parseMillis(datetimeString));
}

From source file:com.facebook.presto.operator.scalar.UnixTimeFunctions.java

License:Apache License

@ScalarFunction
public static long dateParse(Slice dateTime, Slice formatString) {
    DateTimeFormatter formatter = DATETIME_FORMATTER_CACHE.get(formatString);
    return fromMillis(formatter.parseMillis(dateTime.toString(Charsets.UTF_8)));
}

From source file:com.facebook.presto.teradata.functions.TeradataDateFunctions.java

License:Apache License

private static long parseMillis(ConnectorSession session, Slice dateTime, Slice formatString) {
    DateTimeFormatter formatter = DATETIME_FORMATTER_CACHE.get(formatString)
            .withChronology(CHRONOLOGIES[session.getTimeZoneKey().getKey()]).withLocale(session.getLocale());

    try {/*from  w ww .  ja  v  a  2  s .c  om*/
        return formatter.parseMillis(dateTime.toString(UTF_8));
    } catch (IllegalArgumentException e) {
        throw new PrestoException(INVALID_FUNCTION_ARGUMENT, e);
    }
}

From source file:com.linkedin.pinot.core.segment.creator.impl.SegmentColumnarIndexCreator.java

License:Apache License

void writeMetadata() throws ConfigurationException {
    PropertiesConfiguration properties = new PropertiesConfiguration(
            new File(_indexDir, V1Constants.MetadataKeys.METADATA_FILE_NAME));

    properties.setProperty(SEGMENT_CREATOR_VERSION, config.getCreatorVersion());
    properties.setProperty(SEGMENT_PADDING_CHARACTER, String.valueOf(V1Constants.Str.DEFAULT_STRING_PAD_CHAR));
    properties.setProperty(SEGMENT_NAME, segmentName);
    properties.setProperty(TABLE_NAME, config.getTableName());
    properties.setProperty(DIMENSIONS, config.getDimensions());
    properties.setProperty(METRICS, config.getMetrics());
    properties.setProperty(DATETIME_COLUMNS, config.getDateTimeColumnNames());
    properties.setProperty(TIME_COLUMN_NAME, config.getTimeColumnName());
    properties.setProperty(TIME_INTERVAL, "not_there");
    properties.setProperty(SEGMENT_TOTAL_RAW_DOCS, String.valueOf(totalRawDocs));
    properties.setProperty(SEGMENT_TOTAL_AGGREGATE_DOCS, String.valueOf(totalAggDocs));
    properties.setProperty(SEGMENT_TOTAL_DOCS, String.valueOf(totalDocs));
    properties.setProperty(STAR_TREE_ENABLED, String.valueOf(config.isEnableStarTreeIndex()));
    properties.setProperty(SEGMENT_TOTAL_ERRORS, String.valueOf(totalErrors));
    properties.setProperty(SEGMENT_TOTAL_NULLS, String.valueOf(totalNulls));
    properties.setProperty(SEGMENT_TOTAL_CONVERSIONS, String.valueOf(totalConversions));
    properties.setProperty(SEGMENT_TOTAL_NULL_COLS, String.valueOf(totalNullCols));

    StarTreeIndexSpec starTreeIndexSpec = config.getStarTreeIndexSpec();
    if (starTreeIndexSpec != null) {
        properties.setProperty(STAR_TREE_SPLIT_ORDER, starTreeIndexSpec.getDimensionsSplitOrder());
        properties.setProperty(STAR_TREE_MAX_LEAF_RECORDS, starTreeIndexSpec.getMaxLeafRecords());
        properties.setProperty(STAR_TREE_SKIP_STAR_NODE_CREATION_FOR_DIMENSIONS,
                starTreeIndexSpec.getSkipStarNodeCreationForDimensions());
        properties.setProperty(STAR_TREE_SKIP_MATERIALIZATION_CARDINALITY,
                starTreeIndexSpec.getSkipMaterializationCardinalityThreshold());
        properties.setProperty(STAR_TREE_SKIP_MATERIALIZATION_FOR_DIMENSIONS,
                starTreeIndexSpec.getSkipMaterializationForDimensions());
    }/*  ww  w .j  a  va  2s.c om*/

    HllConfig hllConfig = config.getHllConfig();
    Map<String, String> derivedHllFieldToOriginMap = null;
    if (hllConfig != null) {
        properties.setProperty(SEGMENT_HLL_LOG2M, hllConfig.getHllLog2m());
        derivedHllFieldToOriginMap = hllConfig.getDerivedHllFieldToOriginMap();
    }

    // Write time related metadata (start time, end time, time unit)
    String timeColumn = config.getTimeColumnName();
    ColumnIndexCreationInfo timeColumnIndexCreationInfo = indexCreationInfoMap.get(timeColumn);
    if (timeColumnIndexCreationInfo != null) {
        // Use start/end time in config if defined
        if (config.getStartTime() != null && config.getEndTime() != null) {
            properties.setProperty(SEGMENT_START_TIME, config.getStartTime());
            properties.setProperty(SEGMENT_END_TIME, config.getEndTime());
        } else {
            Object minTime = timeColumnIndexCreationInfo.getMin();
            Object maxTime = timeColumnIndexCreationInfo.getMax();

            // Convert time value into millis since epoch for SIMPLE_DATE
            if (config.getTimeColumnType() == SegmentGeneratorConfig.TimeColumnType.SIMPLE_DATE) {
                DateTimeFormatter dateTimeFormatter = DateTimeFormat.forPattern(config.getSimpleDateFormat());
                properties.setProperty(SEGMENT_START_TIME, dateTimeFormatter.parseMillis(minTime.toString()));
                properties.setProperty(SEGMENT_END_TIME, dateTimeFormatter.parseMillis(maxTime.toString()));
            } else {
                properties.setProperty(SEGMENT_START_TIME, minTime);
                properties.setProperty(SEGMENT_END_TIME, maxTime);
            }
        }

        properties.setProperty(TIME_UNIT, config.getSegmentTimeUnit());
    }

    for (Map.Entry<String, String> entry : config.getCustomProperties().entrySet()) {
        properties.setProperty(entry.getKey(), entry.getValue());
    }

    for (Map.Entry<String, ColumnIndexCreationInfo> entry : indexCreationInfoMap.entrySet()) {
        String column = entry.getKey();
        ColumnIndexCreationInfo columnIndexCreationInfo = entry.getValue();
        SegmentDictionaryCreator dictionaryCreator = _dictionaryCreatorMap.get(column);
        int dictionaryElementSize = (dictionaryCreator != null) ? dictionaryCreator.getNumBytesPerEntry() : 0;

        // TODO: after fixing the server-side dependency on HAS_INVERTED_INDEX and deployed, set HAS_INVERTED_INDEX properly
        // The hasInvertedIndex flag in segment metadata is picked up in ColumnMetadata, and will be used during the query
        // plan phase. If it is set to false, then inverted indexes are not used in queries even if they are created via table
        // configs on segment load. So, we set it to true here for now, until we fix the server to update the value inside
        // ColumnMetadata, export information to the query planner that the inverted index available is current and can be used.
        //
        //    boolean hasInvertedIndex = invertedIndexCreatorMap.containsKey();
        boolean hasInvertedIndex = true;

        String hllOriginColumn = null;
        if (derivedHllFieldToOriginMap != null) {
            hllOriginColumn = derivedHllFieldToOriginMap.get(column);
        }

        addColumnMetadataInfo(properties, column, columnIndexCreationInfo, totalDocs, totalRawDocs,
                totalAggDocs, schema.getFieldSpecFor(column), _dictionaryCreatorMap.containsKey(column),
                dictionaryElementSize, hasInvertedIndex, hllOriginColumn);
    }

    properties.save();
}