List of usage examples for org.joda.time.format ISODateTimeFormat dateElementParser
public static DateTimeFormatter dateElementParser()
From source file:de.sub.goobi.metadaten.copier.ComposeFormattedRule.java
License:Open Source License
/** * The function typecast() converts the String arguments so that they can be * used by {@link String#format(String, Object...)}. Only arguments that are * referenced by number can be typecasted. If the format String contains * %2$02d?, the function will convert the second list object to long, if * the format String contains %02d? the function cannot tell which argument * is meant and thus doesnt do anything for it. TODO: check (test) and fix * it - especially catch continue/*from ww w .j ava 2 s. co m*/ * * @param format * format String, to get the desired types from * @param elements * arguments * @return the objects for the format command */ private static Object[] typecast(String format, List<String> elements) { Object[] result = elements.toArray(); Matcher expressions = FORMAT_CODES_SCHEME.matcher(format); while (expressions.find()) { try { int i = Integer.parseInt(expressions.group(1)) - 1; switch (expressions.group(2).codePointAt(0)) { case 'A': result[i] = Double.parseDouble(elements.get(i)); continue; case 'C': result[i] = Integer.parseInt(elements.get(i)); continue; case 'E': case 'G': result[i] = Double.parseDouble(elements.get(i)); continue; case 'T': result[i] = ISODateTimeFormat.dateElementParser().parseMillis(elements.get(i)); continue; case 'X': result[i] = Long.parseLong(elements.get(i)); continue; case 'a': result[i] = Double.parseDouble(elements.get(i)); continue; case 'c': result[i] = Integer.parseInt(elements.get(i)); continue; case 'd': result[i] = Long.parseLong(elements.get(i)); continue; case 'e': case 'f': case 'g': result[i] = Double.parseDouble(elements.get(i)); continue; case 'o': result[i] = Long.parseLong(elements.get(i)); continue; case 't': result[i] = ISODateTimeFormat.dateElementParser().parseMillis(elements.get(i)); continue; case 'x': result[i] = Long.parseLong(elements.get(i)); } } catch (ArrayIndexOutOfBoundsException | ClassCastException | NumberFormatException e) { } } return result; }
From source file:io.prestosql.operator.scalar.DateTimeFunctions.java
License:Apache License
@ScalarFunction("from_iso8601_date") @LiteralParameters("x") @SqlType(StandardTypes.DATE)/*from w w w. j a v a 2 s. co m*/ public static long fromISO8601Date(ConnectorSession session, @SqlType("varchar(x)") Slice iso8601DateTime) { DateTimeFormatter formatter = ISODateTimeFormat.dateElementParser().withChronology(UTC_CHRONOLOGY); DateTime dateTime = parseDateTimeHelper(formatter, iso8601DateTime.toStringUtf8()); return MILLISECONDS.toDays(dateTime.getMillis()); }
From source file:org.apache.druid.java.util.common.parsers.TimestampParser.java
License:Apache License
private static DateTimeFormatter createAutoParser() { final DateTimeFormatter offsetElement = new DateTimeFormatterBuilder().appendTimeZoneOffset("Z", true, 2, 4) .toFormatter();//ww w .j av a 2 s . c o m DateTimeParser timeOrOffset = new DateTimeFormatterBuilder() .append(null, new DateTimeParser[] { new DateTimeFormatterBuilder().appendLiteral('T').toParser(), new DateTimeFormatterBuilder().appendLiteral(' ').toParser() }) .appendOptional(ISODateTimeFormat.timeElementParser().getParser()) .appendOptional(offsetElement.getParser()).toParser(); return new DateTimeFormatterBuilder().append(ISODateTimeFormat.dateElementParser()) .appendOptional(timeOrOffset).toFormatter(); }
From source file:org.apache.druid.query.expression.TimestampParseExprMacro.java
License:Apache License
/** * Default formatter that parses according to the docs for this method: "If the pattern is not provided, this parses * time strings in either ISO8601 or SQL format." *///from w w w .j a v a2 s.c o m private static DateTimes.UtcFormatter createDefaultParser(final DateTimeZone timeZone) { final DateTimeFormatter offsetElement = new DateTimeFormatterBuilder().appendTimeZoneOffset("Z", true, 2, 4) .toFormatter(); DateTimeParser timeOrOffset = new DateTimeFormatterBuilder() .append(null, new DateTimeParser[] { new DateTimeFormatterBuilder().appendLiteral('T').toParser(), new DateTimeFormatterBuilder().appendLiteral(' ').toParser() }) .appendOptional(ISODateTimeFormat.timeElementParser().getParser()) .appendOptional(offsetElement.getParser()).toParser(); return DateTimes.wrapFormatter(new DateTimeFormatterBuilder().append(ISODateTimeFormat.dateElementParser()) .appendOptional(timeOrOffset).toFormatter().withZone(timeZone)); }
From source file:org.kitodo.production.metadata.copier.ComposeFormattedRule.java
License:Open Source License
/** * The function typecast() converts the String arguments so that they can be * used by {@link String#format(String, Object...)}. Only arguments that are * referenced by number can be typecasted. If the format String contains * %2$02d?, the function will convert the second list object to long, if * the format String contains %02d? the function cannot tell which argument * is meant and thus doesnt do anything for it. TODO: check (test) and fix * it - especially catch continue/*from w ww . ja va2s .co m*/ * * @param format * format String, to get the desired types from * @param elements * arguments * @return the objects for the format command */ private static Object[] typecast(String format, List<String> elements) { Object[] result = elements.toArray(); Matcher expressions = FORMAT_CODES_SCHEME.matcher(format); while (expressions.find()) { try { int i = Integer.parseInt(expressions.group(1)) - 1; switch (expressions.group(2).codePointAt(0)) { case 'A': case 'E': case 'G': case 'a': case 'e': case 'f': case 'g': result[i] = Double.parseDouble(elements.get(i)); continue; case 'C': case 'c': result[i] = Integer.parseInt(elements.get(i)); continue; case 'T': case 't': result[i] = ISODateTimeFormat.dateElementParser().parseMillis(elements.get(i)); continue; case 'X': case 'd': case 'o': result[i] = Long.parseLong(elements.get(i)); continue; case 'x': result[i] = Long.parseLong(elements.get(i)); } } catch (ArrayIndexOutOfBoundsException | ClassCastException | NumberFormatException e) { logger.info(e); } } return result; }
From source file:org.openehr.rm.datatypes.quantity.datetime.DvDateTimeParser.java
License:LGPL
public static DateTime parseDate(String value) { if (value == null) { throw new IllegalArgumentException("null value for date"); }//from w ww. j a v a 2 s. c o m if (!value.matches(EXT_DATE) && !value.matches(BDATE)) { throw new IllegalArgumentException("invalid pattern for date: " + value); } DateTime dt = null; if (value.indexOf("-") > 0 || value.length() == 4) { try { dt = ISODateTimeFormat.dateElementParser().withOffsetParsed().parseDateTime(value); } catch (Exception e) { throw new IllegalArgumentException("invalid value for extended date: " + value); } } else { int size = analyseDateString(value); DateTimeFormatter formatter = null; switch (size) { case 1: formatter = ISODateTimeFormat.year().withOffsetParsed(); break; case 2: formatter = DateTimeFormat.forPattern("yyyyMM").withOffsetParsed(); break; case 3: formatter = ISODateTimeFormat.basicDate().withOffsetParsed(); break; } try { dt = formatter.parseDateTime(value); } catch (Exception e) { throw new IllegalArgumentException("invalid value for basic date: " + value); } } return dt; }