Java Code Examples for org.joda.time.format.DateTimeFormatter#parseMillis()

The following examples show how to use org.joda.time.format.DateTimeFormatter#parseMillis() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DateTimeFunctions.java    From presto with Apache License 2.0 6 votes vote down vote up
@ScalarFunction
@LiteralParameters({"x", "y"})
@SqlType("timestamp(3)") // TODO: increase precision?
public static long dateParse(ConnectorSession session, @SqlType("varchar(x)") Slice dateTime, @SqlType("varchar(y)") Slice formatString)
{
    DateTimeFormatter formatter = DATETIME_FORMATTER_CACHE.get(formatString)
            .withChronology(session.isLegacyTimestamp() ? getChronology(session.getTimeZoneKey()) : UTC_CHRONOLOGY)
            .withLocale(session.getLocale());

    try {
        return formatter.parseMillis(dateTime.toStringUtf8());
    }
    catch (IllegalArgumentException e) {
        throw new PrestoException(INVALID_FUNCTION_ARGUMENT, e);
    }
}
 
Example 2
Source File: CustomDateTimeJsonKinesisFieldDecoder.java    From presto-kinesis with Apache License 2.0 6 votes vote down vote up
@Override
public long getLong()
{
    if (isNull()) {
        return 0L;
    }

    if (value.canConvertToLong()) {
        return value.asLong();
    }

    checkNotNull(columnHandle.getFormatHint(), "formatHint is null");
    String textValue = value.isValueNode() ? value.asText() : value.toString();

    DateTimeFormatter formatter = DateTimeFormat.forPattern(columnHandle.getFormatHint()).withLocale(Locale.ENGLISH).withZoneUTC();
    return formatter.parseMillis(textValue);
}
 
Example 3
Source File: TestBucketPath.java    From mt-flume with Apache License 2.0 6 votes vote down vote up
@Test
public void testDateRace() {
  Clock mockClock = mock(Clock.class);
  DateTimeFormatter parser = ISODateTimeFormat.dateTimeParser();
  long two = parser.parseMillis("2013-04-21T02:59:59-00:00");
  long three = parser.parseMillis("2013-04-21T03:00:00-00:00");
  when(mockClock.currentTimeMillis()).thenReturn(two, three);

  // save & modify static state (yuck)
  Clock origClock = BucketPath.getClock();
  BucketPath.setClock(mockClock);

  String pat = "%H:%M";
  String escaped = BucketPath.escapeString(pat,
      new HashMap<String, String>(),
      TimeZone.getTimeZone("UTC"), true, Calendar.MINUTE, 10, true);

  // restore static state
  BucketPath.setClock(origClock);

  Assert.assertEquals("Race condition detected", "02:50", escaped);
}
 
Example 4
Source File: TimeRangeValuePredicate.java    From suro with Apache License 2.0 6 votes vote down vote up
public TimeRangeValuePredicate(String timeFormat, String start, String end){
	
	Preconditions.checkArgument(!Strings.isNullOrEmpty(timeFormat), "Time format can't be null or empty. ");
       Preconditions.checkArgument(!Strings.isNullOrEmpty(start), "The lower bound of time range can't be null or empty.");
	Preconditions.checkArgument(!Strings.isNullOrEmpty(end), "The upper bound of time range  can't be null or empty. ");
	
	this.timeFormat = timeFormat;
	this.start = start;
	this.end = end; 
	
	DateTimeFormatter format = TimeUtil.toDateTimeFormatter("time format", timeFormat);
	long startInstant = format.parseMillis(start);
	long endInstant = format.parseMillis(end);
	
	try{
		this.interval = new Interval(startInstant, endInstant);
	}catch(IllegalArgumentException e) {
		String msg = String.format(
			"The lower bound should be smaller than or equal to upper bound for a time range. Given range: [%s, %s)",
			start,
			end);
		IllegalArgumentException iae = new IllegalArgumentException(msg, e.getCause());
		iae.setStackTrace(e.getStackTrace());
		throw iae;
	}
}
 
Example 5
Source File: TeradataDateFunctions.java    From presto with Apache License 2.0 5 votes vote down vote up
private static long parseMillis(TimeZoneKey timeZoneKey, Locale locale, Slice dateTime, Slice formatString)
{
    DateTimeFormatter formatter = DATETIME_FORMATTER_CACHE.get(formatString)
            .withChronology(CHRONOLOGIES[timeZoneKey.getKey()])
            .withLocale(locale);

    try {
        return formatter.parseMillis(dateTime.toString(UTF_8));
    }
    catch (IllegalArgumentException e) {
        throw new PrestoException(INVALID_FUNCTION_ARGUMENT, e);
    }
}
 
Example 6
Source File: TemplateUtils.java    From qconfig with MIT License 5 votes vote down vote up
private static long parseDataTime(DateTimeFormatter dataTimeFormatter, String valueStr) {
    try {
        return Long.parseLong(valueStr);
    } catch (Exception e) {
        return dataTimeFormatter.parseMillis(valueStr);
    }
}
 
Example 7
Source File: DateMathParser.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
private long parseDateTime(String value, DateTimeZone timeZone) {
    DateTimeFormatter parser = dateTimeFormatter.parser();
    if (timeZone != null) {
        parser = parser.withZone(timeZone);
    }
    try {
        return parser.parseMillis(value);
    } catch (IllegalArgumentException e) {
        
        throw new ElasticsearchParseException("failed to parse date field [{}] with format [{}]", e, value, dateTimeFormatter.format());
    }
}
 
Example 8
Source File: TimeMillisValuePredicate.java    From suro with Apache License 2.0 5 votes vote down vote up
public TimeMillisValuePredicate(String timeFormat, String value, String fnName){
	this.timeFormat = timeFormat;
	this.value = value;
	this.fnName = fnName; 
	
	DateTimeFormatter formatter = TimeUtil.toDateTimeFormatter("time format", timeFormat);
	
	long timeInMs = formatter.parseMillis(value);
	this.longPredicate = new NumericValuePredicate(timeInMs, fnName);
}
 
Example 9
Source File: DateTimePatternHandler.java    From incubator-pinot with Apache License 2.0 4 votes vote down vote up
/**
 * Converts the dateTimeString of passed pattern into a long of the millis since epoch
 */
public static Long parseDateTimeStringToEpochMillis(String dateTimeString, String pattern) {
  DateTimeFormatter dateTimeFormatter = getDateTimeFormatter(pattern);
  return dateTimeFormatter.parseMillis(dateTimeString);
}