final DateTimeFormatter df = DateTimeFormat .forPattern("EEE MMM dd HH:mm:ss 'GMT'Z yyyy"); final DateTime dateTime = df.withOffsetParsed() .parseDateTime("Mon Aug 24 12:36:46 GMT+1000 2009");
private DateTime parseDateTime(String s) { if (Strings.isNullOrEmpty(s)) { throw new IllegalArgumentException("Null or empty string"); } final DateTimeFormatter formatter; if (s.contains("T")) { formatter = ISODateTimeFormat.dateTime(); } else { formatter = Tools.timeFormatterWithOptionalMilliseconds(); } // Use withOffsetParsed() to keep the timezone! return formatter.withOffsetParsed().parseDateTime(s); } }
public static void testDate() { DateTimeFormatter df = DateTimeFormat.forPattern("dd MM yyyy HH:mm:ss.SSS Z"); DateTime temp = df.withOffsetParsed().parseDateTime("30 11 2012 12:08:56.235 +0700"); DateTimeZone theZone = temp.getZone(); Date date = temp.toDate(); DateTime dateTime = new DateTime(date); DateTimeFormatter df2 = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss.SSZZ"); DateTimeFormatter df3 = df2.withZone(theZone); System.out.println(dateTime.toString(df2)); System.out.println(dateTime.toString(df3)); }
/** * Parse a string (optionally containing a zone) as a value of TIMESTAMP WITH TIME ZONE type. * If the string doesn't specify a zone, it is interpreted in {@code timeZoneKey} zone. * <p> * For example: {@code "2000-01-01 01:23:00"} is parsed to TIMESTAMP WITH TIME ZONE * {@code 2000-01-01T01:23:00 <provided zone>} and {@code "2000-01-01 01:23:00 +01:23"} * is parsed to TIMESTAMP WITH TIME ZONE {@code 2000-01-01T01:23:00.000+01:23}. * * @return stack representation of TIMESTAMP WITH TIME ZONE type */ public static long parseTimestampWithTimeZone(TimeZoneKey timeZoneKey, String timestampWithTimeZone) { DateTime dateTime = TIMESTAMP_WITH_OR_WITHOUT_TIME_ZONE_FORMATTER.withChronology(getChronology(timeZoneKey)).withOffsetParsed().parseDateTime(timestampWithTimeZone); return packDateTimeWithZone(dateTime); }
/** * Parses a {@code DateTime} from the specified string. * <p> * This uses {@link ISODateTimeFormat#dateTimeParser()}{@code .withOffsetParsed()} * which is different to passing a {@code String} to the constructor. * <p> * Sometimes this method and {@code new DateTime(str)} return different results. * This can be confusing as the difference is not visible in {@link #toString()}. * <p> * When passed a date-time string without an offset, such as '2010-06-30T01:20', * both the constructor and this method use the default time-zone. * As such, {@code DateTime.parse("2010-06-30T01:20")} and * {@code new DateTime("2010-06-30T01:20"))} are equal. * <p> * However, when this method is passed a date-time string with an offset, * the offset is directly parsed and stored. * As such, {@code DateTime.parse("2010-06-30T01:20+02:00")} and * {@code new DateTime("2010-06-30T01:20+02:00"))} are NOT equal. * The object produced via this method has a zone of {@code DateTimeZone.forOffsetHours(2)}. * The object produced via the constructor has a zone of {@code DateTimeZone.getDefault()}. * * @param str the string to parse, not null * @since 2.0 */ @FromString public static DateTime parse(String str) { return parse(str, ISODateTimeFormat.dateTimeParser().withOffsetParsed()); }
/** * Parses a {@code MutableDateTime} from the specified string. * <p> * This uses {@link ISODateTimeFormat#dateTimeParser()}. * * @param str the string to parse, not null * @since 2.0 */ @FromString public static MutableDateTime parse(String str) { return parse(str, ISODateTimeFormat.dateTimeParser().withOffsetParsed()); }
/** * Parses a {@code DateMidnight} from the specified string. * <p> * This uses {@link ISODateTimeFormat#dateTimeParser()}. * * @param str the string to parse, not null * @since 2.0 */ @FromString public static DateMidnight parse(String str) { return parse(str, ISODateTimeFormat.dateTimeParser().withOffsetParsed()); }
/** * Parses a {@code DateTime} from the specified string. * <p> * This uses {@link ISODateTimeFormat#dateTimeParser()}{@code .withOffsetParsed()} * which is different to passing a {@code String} to the constructor. * <p> * Sometimes this method and {@code new DateTime(str)} return different results. * This can be confusing as the difference is not visible in {@link #toString()}. * <p> * When passed a date-time string without an offset, such as '2010-06-30T01:20', * both the constructor and this method use the default time-zone. * As such, {@code DateTime.parse("2010-06-30T01:20")} and * {@code new DateTime("2010-06-30T01:20"))} are equal. * <p> * However, when this method is passed a date-time string with an offset, * the offset is directly parsed and stored. * As such, {@code DateTime.parse("2010-06-30T01:20+02:00")} and * {@code new DateTime("2010-06-30T01:20+02:00"))} are NOT equal. * The object produced via this method has a zone of {@code DateTimeZone.forOffsetHours(2)}. * The object produced via the constructor has a zone of {@code DateTimeZone.getDefault()}. * * @param str the string to parse, not null * @since 2.0 */ @FromString public static DateTime parse(String str) { return parse(str, ISODateTimeFormat.dateTimeParser().withOffsetParsed()); }
/** * Parses a {@code MutableDateTime} from the specified string. * <p> * This uses {@link ISODateTimeFormat#dateTimeParser()}. * * @param str the string to parse, not null * @since 2.0 */ @FromString public static MutableDateTime parse(String str) { return parse(str, ISODateTimeFormat.dateTimeParser().withOffsetParsed()); }
/** * Parses a {@code DateMidnight} from the specified string. * <p> * This uses {@link ISODateTimeFormat#dateTimeParser()}. * * @param str the string to parse, not null * @since 2.0 */ @FromString public static DateMidnight parse(String str) { return parse(str, ISODateTimeFormat.dateTimeParser().withOffsetParsed()); }
DateTimeFormatter dateTimeParser = ISODateTimeFormat.dateTimeParser().withOffsetParsed(); PeriodFormatter periodParser = ISOPeriodFormat.standard(); DateTime start = null;
DateTimeFormatter dateTimeParser = ISODateTimeFormat.dateTimeParser().withOffsetParsed(); PeriodFormatter periodParser = ISOPeriodFormat.standard(); DateTime start = null;
@ScalarFunction("from_iso8601_timestamp") @LiteralParameters("x") @SqlType(StandardTypes.TIMESTAMP_WITH_TIME_ZONE) public static long fromISO8601Timestamp(ConnectorSession session, @SqlType("varchar(x)") Slice iso8601DateTime) { DateTimeFormatter formatter = ISODateTimeFormat.dateTimeParser() .withChronology(getChronology(session.getTimeZoneKey())) .withOffsetParsed(); return packDateTimeWithZone(parseDateTimeHelper(formatter, iso8601DateTime.toStringUtf8())); }
@Description("parses the specified date/time by the given format") @ScalarFunction @LiteralParameters({"x", "y"}) @SqlType(StandardTypes.TIMESTAMP_WITH_TIME_ZONE) public static long parseDatetime(ConnectorSession session, @SqlType("varchar(x)") Slice datetime, @SqlType("varchar(y)") Slice formatString) { try { return packDateTimeWithZone(parseDateTimeHelper( DateTimeFormat.forPattern(formatString.toStringUtf8()) .withChronology(getChronology(session.getTimeZoneKey())) .withOffsetParsed() .withLocale(session.getLocale()), datetime.toStringUtf8())); } catch (IllegalArgumentException e) { throw new PrestoException(INVALID_FUNCTION_ARGUMENT, e); } }
private DateTime getParsedDateTime(String value) { try { return ISODateTimeFormat.dateTimeParser().withOffsetParsed().parseDateTime(value); } catch (DateTimeParseException e) { throw new IllegalArgumentException(format("Could not parse value '%s' according to ISO 8601", value)); } } }
/** * Parses a {@code DateTime} from the specified string. * <p> * This uses {@link ISODateTimeFormat#dateTimeParser()}. * * @param str the string to parse, not null * @since 2.0 */ @FromString public static DateTime parse(String str) { return parse(str, ISODateTimeFormat.dateTimeParser().withOffsetParsed()); }
.append(_format.createFormatter(provider).withOffsetParsed().print(value)); sb = sb.append('[') .append(value.getZone())
/** * Parses a {@code MutableDateTime} from the specified string. * <p> * This uses {@link ISODateTimeFormat#dateTimeParser()}. * * @param str the string to parse, not null * @since 2.0 */ @FromString public static MutableDateTime parse(String str) { return parse(str, ISODateTimeFormat.dateTimeParser().withOffsetParsed()); }
/** * Parses a {@code DateMidnight} from the specified string. * <p> * This uses {@link ISODateTimeFormat#dateTimeParser()}. * * @param str the string to parse, not null * @since 2.0 */ @FromString public static DateMidnight parse(String str) { return parse(str, ISODateTimeFormat.dateTimeParser().withOffsetParsed()); }
/** * Accessor used during deserialization. */ public DateTimeFormatter createParser(DeserializationContext ctxt) { DateTimeFormatter formatter = _formatter; if (!_explicitLocale) { Locale loc = ctxt.getLocale(); if (loc != null && !loc.equals(_locale)) { formatter = formatter.withLocale(loc); } } if (!_explicitTimezone) { if (shouldAdjustToContextTimeZone(ctxt)) { TimeZone tz = ctxt.getTimeZone(); if (tz != null && !tz.equals(_jdkTimezone)) { formatter = formatter.withZone(DateTimeZone.forTimeZone(tz)); } } else { formatter = formatter.withOffsetParsed(); } } return formatter; }