List of usage examples for org.joda.time.format DateTimeFormatter withZone
public DateTimeFormatter withZone(DateTimeZone zone)
From source file:net.bashtech.geobot.MessageReplaceParser.java
License:Open Source License
public static String handleDatetime(String message, String prefix, String suffix, String format) { int commandStart = message.indexOf(prefix); int commandEnd = message.indexOf(suffix); String replaced = message.substring(commandStart, commandEnd + suffix.length()); DateTimeZone tz;// ww w .j a va2 s. c om if (commandStart + prefix.length() < commandEnd) { String tzid = message.substring(commandStart + prefix.length(), commandEnd); try { tz = DateTimeZone.forID(tzid); } catch (IllegalArgumentException e) { tz = DateTimeZone.UTC; } } else { tz = DateTimeZone.UTC; } DateTimeFormatter fmt = DateTimeFormat.forPattern(format); fmt = fmt.withZone(tz); String dateStr = fmt.print(new DateTime()); message = message.replace(replaced, dateStr); return message; }
From source file:org.apache.isis.objectstore.sql.jdbc.JdbcResults.java
License:Apache License
@Override public java.sql.Time getJavaTimeOnly(final String columnName) { try {//from ww w . j a v a2s.co m final String string = set.getString(columnName); final DateTimeFormatter formatter = DateTimeFormat.forPattern("HH:mm:ss"); final DateTime utcDate = formatter.withZone(Defaults.getTimeZone()).parseDateTime(string); final java.sql.Time time = new java.sql.Time(utcDate.getMillis()); return time; } catch (final SQLException e) { throw new SqlObjectStoreException(e); } }
From source file:org.apache.isis.objectstore.sql.jdbc.JdbcResults.java
License:Apache License
@Override public Time getTime(final String columnName) { try {/*from www . ja v a 2 s . co m*/ final String string = set.getString(columnName); final DateTimeFormatter formatter = DateTimeFormat.forPattern("HH:mm:ss"); final DateTimeZone defaultTimeZone = Defaults.getTimeZone(); final DateTime utcDate = formatter.withZone(defaultTimeZone).parseDateTime(string); return new Time(utcDate); } catch (final SQLException e) { throw new SqlObjectStoreException(e); } }
From source file:org.apache.pig.builtin.ToDate3ARGS.java
License:Apache License
public DateTime exec(Tuple input) throws IOException { if (input == null || input.size() < 1 || input.get(0) == null) { return null; }/*from w w w . j ava2 s. c o m*/ DateTimeFormatter dtf = DateTimeFormat.forPattern(DataType.toString(input.get(1))); DateTimeZone dtz = DateTimeZone .forOffsetMillis(DateTimeZone.forID(DataType.toString(input.get(2))).getOffset(null)); return dtf.withZone(dtz).parseDateTime(DataType.toString(input.get(0))); }
From source file:org.apache.wicket.datetime.DateConverter.java
License:Apache License
/** * @see org.apache.wicket.util.convert.IConverter#convertToObject(java.lang.String, * java.util.Locale)/* ww w. java 2s . co m*/ */ public Date convertToObject(String value, Locale locale) { if (Strings.isEmpty(value)) { return null; } DateTimeFormatter format = getFormat(locale); if (format == null) { throw new IllegalStateException("format must be not null"); } if (applyTimeZoneDifference) { TimeZone zone = getClientTimeZone(); DateTime dateTime = null; // set time zone for client format = format.withZone(getTimeZone()); try { // parse date retaining the time of the submission dateTime = format.parseDateTime(value); } catch (RuntimeException e) { throw new ConversionException(e); } // apply the server time zone to the parsed value if (zone != null) { dateTime = dateTime.withZoneRetainFields(DateTimeZone.forTimeZone(zone)); } return dateTime.toDate(); } else { try { DateTime date = format.parseDateTime(value); return date.toDate(); } catch (RuntimeException e) { throw new ConversionException(e); } } }
From source file:org.apache.wicket.datetime.DateConverter.java
License:Apache License
/** * @see org.apache.wicket.util.convert.IConverter#convertToString(java.lang.Object, * java.util.Locale)/*from w ww. j a va 2 s . c o m*/ */ public String convertToString(Date value, Locale locale) { DateTime dt = new DateTime((value).getTime(), getTimeZone()); DateTimeFormatter format = getFormat(locale); if (applyTimeZoneDifference) { TimeZone zone = getClientTimeZone(); if (zone != null) { // apply time zone to formatter format = format.withZone(DateTimeZone.forTimeZone(zone)); } } return format.print(dt); }
From source file:org.codelibs.elasticsearch.common.joda.DateMathParser.java
License:Apache License
private long parseDateTime(String value, DateTimeZone timeZone, boolean roundUpIfNoTime) { DateTimeFormatter parser = dateTimeFormatter.parser(); if (timeZone != null) { parser = parser.withZone(timeZone); }// ww w .j a v a2s .c om try { MutableDateTime date; // We use 01/01/1970 as a base date so that things keep working with date // fields that are filled with times without dates if (roundUpIfNoTime) { date = new MutableDateTime(1970, 1, 1, 23, 59, 59, 999, DateTimeZone.UTC); } else { date = new MutableDateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeZone.UTC); } final int end = parser.parseInto(date, value, 0); if (end < 0) { int position = ~end; throw new IllegalArgumentException("Parse failure at index [" + position + "] of [" + value + "]"); } else if (end != value.length()) { throw new IllegalArgumentException( "Unrecognized chars at the end of [" + value + "]: [" + value.substring(end) + "]"); } return date.getMillis(); } catch (IllegalArgumentException e) { throw new ElasticsearchParseException("failed to parse date field [{}] with format [{}]", e, value, dateTimeFormatter.format()); } }
From source file:org.codelibs.elasticsearch.common.joda.Joda.java
License:Apache License
/** * Parses a joda based pattern, including some named ones (similar to the built in Joda ISO ones). *///from w w w .j ava 2 s . c om public static FormatDateTimeFormatter forPattern(String input, Locale locale) { if (Strings.hasLength(input)) { input = input.trim(); } if (input == null || input.length() == 0) { throw new IllegalArgumentException("No date pattern provided"); } DateTimeFormatter formatter; if ("basicDate".equals(input) || "basic_date".equals(input)) { formatter = ISODateTimeFormat.basicDate(); } else if ("basicDateTime".equals(input) || "basic_date_time".equals(input)) { formatter = ISODateTimeFormat.basicDateTime(); } else if ("basicDateTimeNoMillis".equals(input) || "basic_date_time_no_millis".equals(input)) { formatter = ISODateTimeFormat.basicDateTimeNoMillis(); } else if ("basicOrdinalDate".equals(input) || "basic_ordinal_date".equals(input)) { formatter = ISODateTimeFormat.basicOrdinalDate(); } else if ("basicOrdinalDateTime".equals(input) || "basic_ordinal_date_time".equals(input)) { formatter = ISODateTimeFormat.basicOrdinalDateTime(); } else if ("basicOrdinalDateTimeNoMillis".equals(input) || "basic_ordinal_date_time_no_millis".equals(input)) { formatter = ISODateTimeFormat.basicOrdinalDateTimeNoMillis(); } else if ("basicTime".equals(input) || "basic_time".equals(input)) { formatter = ISODateTimeFormat.basicTime(); } else if ("basicTimeNoMillis".equals(input) || "basic_time_no_millis".equals(input)) { formatter = ISODateTimeFormat.basicTimeNoMillis(); } else if ("basicTTime".equals(input) || "basic_t_Time".equals(input)) { formatter = ISODateTimeFormat.basicTTime(); } else if ("basicTTimeNoMillis".equals(input) || "basic_t_time_no_millis".equals(input)) { formatter = ISODateTimeFormat.basicTTimeNoMillis(); } else if ("basicWeekDate".equals(input) || "basic_week_date".equals(input)) { formatter = ISODateTimeFormat.basicWeekDate(); } else if ("basicWeekDateTime".equals(input) || "basic_week_date_time".equals(input)) { formatter = ISODateTimeFormat.basicWeekDateTime(); } else if ("basicWeekDateTimeNoMillis".equals(input) || "basic_week_date_time_no_millis".equals(input)) { formatter = ISODateTimeFormat.basicWeekDateTimeNoMillis(); } else if ("date".equals(input)) { formatter = ISODateTimeFormat.date(); } else if ("dateHour".equals(input) || "date_hour".equals(input)) { formatter = ISODateTimeFormat.dateHour(); } else if ("dateHourMinute".equals(input) || "date_hour_minute".equals(input)) { formatter = ISODateTimeFormat.dateHourMinute(); } else if ("dateHourMinuteSecond".equals(input) || "date_hour_minute_second".equals(input)) { formatter = ISODateTimeFormat.dateHourMinuteSecond(); } else if ("dateHourMinuteSecondFraction".equals(input) || "date_hour_minute_second_fraction".equals(input)) { formatter = ISODateTimeFormat.dateHourMinuteSecondFraction(); } else if ("dateHourMinuteSecondMillis".equals(input) || "date_hour_minute_second_millis".equals(input)) { formatter = ISODateTimeFormat.dateHourMinuteSecondMillis(); } else if ("dateOptionalTime".equals(input) || "date_optional_time".equals(input)) { // in this case, we have a separate parser and printer since the dataOptionalTimeParser can't print // this sucks we should use the root local by default and not be dependent on the node return new FormatDateTimeFormatter(input, ISODateTimeFormat.dateOptionalTimeParser().withZone(DateTimeZone.UTC), ISODateTimeFormat.dateTime().withZone(DateTimeZone.UTC), locale); } else if ("dateTime".equals(input) || "date_time".equals(input)) { formatter = ISODateTimeFormat.dateTime(); } else if ("dateTimeNoMillis".equals(input) || "date_time_no_millis".equals(input)) { formatter = ISODateTimeFormat.dateTimeNoMillis(); } else if ("hour".equals(input)) { formatter = ISODateTimeFormat.hour(); } else if ("hourMinute".equals(input) || "hour_minute".equals(input)) { formatter = ISODateTimeFormat.hourMinute(); } else if ("hourMinuteSecond".equals(input) || "hour_minute_second".equals(input)) { formatter = ISODateTimeFormat.hourMinuteSecond(); } else if ("hourMinuteSecondFraction".equals(input) || "hour_minute_second_fraction".equals(input)) { formatter = ISODateTimeFormat.hourMinuteSecondFraction(); } else if ("hourMinuteSecondMillis".equals(input) || "hour_minute_second_millis".equals(input)) { formatter = ISODateTimeFormat.hourMinuteSecondMillis(); } else if ("ordinalDate".equals(input) || "ordinal_date".equals(input)) { formatter = ISODateTimeFormat.ordinalDate(); } else if ("ordinalDateTime".equals(input) || "ordinal_date_time".equals(input)) { formatter = ISODateTimeFormat.ordinalDateTime(); } else if ("ordinalDateTimeNoMillis".equals(input) || "ordinal_date_time_no_millis".equals(input)) { formatter = ISODateTimeFormat.ordinalDateTimeNoMillis(); } else if ("time".equals(input)) { formatter = ISODateTimeFormat.time(); } else if ("timeNoMillis".equals(input) || "time_no_millis".equals(input)) { formatter = ISODateTimeFormat.timeNoMillis(); } else if ("tTime".equals(input) || "t_time".equals(input)) { formatter = ISODateTimeFormat.tTime(); } else if ("tTimeNoMillis".equals(input) || "t_time_no_millis".equals(input)) { formatter = ISODateTimeFormat.tTimeNoMillis(); } else if ("weekDate".equals(input) || "week_date".equals(input)) { formatter = ISODateTimeFormat.weekDate(); } else if ("weekDateTime".equals(input) || "week_date_time".equals(input)) { formatter = ISODateTimeFormat.weekDateTime(); } else if ("weekDateTimeNoMillis".equals(input) || "week_date_time_no_millis".equals(input)) { formatter = ISODateTimeFormat.weekDateTimeNoMillis(); } else if ("weekyear".equals(input) || "week_year".equals(input)) { formatter = ISODateTimeFormat.weekyear(); } else if ("weekyearWeek".equals(input) || "weekyear_week".equals(input)) { formatter = ISODateTimeFormat.weekyearWeek(); } else if ("weekyearWeekDay".equals(input) || "weekyear_week_day".equals(input)) { formatter = ISODateTimeFormat.weekyearWeekDay(); } else if ("year".equals(input)) { formatter = ISODateTimeFormat.year(); } else if ("yearMonth".equals(input) || "year_month".equals(input)) { formatter = ISODateTimeFormat.yearMonth(); } else if ("yearMonthDay".equals(input) || "year_month_day".equals(input)) { formatter = ISODateTimeFormat.yearMonthDay(); } else if ("epoch_second".equals(input)) { formatter = new DateTimeFormatterBuilder() .append(new EpochTimePrinter(false), new EpochTimeParser(false)).toFormatter(); } else if ("epoch_millis".equals(input)) { formatter = new DateTimeFormatterBuilder().append(new EpochTimePrinter(true), new EpochTimeParser(true)) .toFormatter(); // strict date formats here, must be at least 4 digits for year and two for months and two for day } else if ("strictBasicWeekDate".equals(input) || "strict_basic_week_date".equals(input)) { formatter = StrictISODateTimeFormat.basicWeekDate(); } else if ("strictBasicWeekDateTime".equals(input) || "strict_basic_week_date_time".equals(input)) { formatter = StrictISODateTimeFormat.basicWeekDateTime(); } else if ("strictBasicWeekDateTimeNoMillis".equals(input) || "strict_basic_week_date_time_no_millis".equals(input)) { formatter = StrictISODateTimeFormat.basicWeekDateTimeNoMillis(); } else if ("strictDate".equals(input) || "strict_date".equals(input)) { formatter = StrictISODateTimeFormat.date(); } else if ("strictDateHour".equals(input) || "strict_date_hour".equals(input)) { formatter = StrictISODateTimeFormat.dateHour(); } else if ("strictDateHourMinute".equals(input) || "strict_date_hour_minute".equals(input)) { formatter = StrictISODateTimeFormat.dateHourMinute(); } else if ("strictDateHourMinuteSecond".equals(input) || "strict_date_hour_minute_second".equals(input)) { formatter = StrictISODateTimeFormat.dateHourMinuteSecond(); } else if ("strictDateHourMinuteSecondFraction".equals(input) || "strict_date_hour_minute_second_fraction".equals(input)) { formatter = StrictISODateTimeFormat.dateHourMinuteSecondFraction(); } else if ("strictDateHourMinuteSecondMillis".equals(input) || "strict_date_hour_minute_second_millis".equals(input)) { formatter = StrictISODateTimeFormat.dateHourMinuteSecondMillis(); } else if ("strictDateOptionalTime".equals(input) || "strict_date_optional_time".equals(input)) { // in this case, we have a separate parser and printer since the dataOptionalTimeParser can't print // this sucks we should use the root local by default and not be dependent on the node return new FormatDateTimeFormatter(input, StrictISODateTimeFormat.dateOptionalTimeParser().withZone(DateTimeZone.UTC), StrictISODateTimeFormat.dateTime().withZone(DateTimeZone.UTC), locale); } else if ("strictDateTime".equals(input) || "strict_date_time".equals(input)) { formatter = StrictISODateTimeFormat.dateTime(); } else if ("strictDateTimeNoMillis".equals(input) || "strict_date_time_no_millis".equals(input)) { formatter = StrictISODateTimeFormat.dateTimeNoMillis(); } else if ("strictHour".equals(input) || "strict_hour".equals(input)) { formatter = StrictISODateTimeFormat.hour(); } else if ("strictHourMinute".equals(input) || "strict_hour_minute".equals(input)) { formatter = StrictISODateTimeFormat.hourMinute(); } else if ("strictHourMinuteSecond".equals(input) || "strict_hour_minute_second".equals(input)) { formatter = StrictISODateTimeFormat.hourMinuteSecond(); } else if ("strictHourMinuteSecondFraction".equals(input) || "strict_hour_minute_second_fraction".equals(input)) { formatter = StrictISODateTimeFormat.hourMinuteSecondFraction(); } else if ("strictHourMinuteSecondMillis".equals(input) || "strict_hour_minute_second_millis".equals(input)) { formatter = StrictISODateTimeFormat.hourMinuteSecondMillis(); } else if ("strictOrdinalDate".equals(input) || "strict_ordinal_date".equals(input)) { formatter = StrictISODateTimeFormat.ordinalDate(); } else if ("strictOrdinalDateTime".equals(input) || "strict_ordinal_date_time".equals(input)) { formatter = StrictISODateTimeFormat.ordinalDateTime(); } else if ("strictOrdinalDateTimeNoMillis".equals(input) || "strict_ordinal_date_time_no_millis".equals(input)) { formatter = StrictISODateTimeFormat.ordinalDateTimeNoMillis(); } else if ("strictTime".equals(input) || "strict_time".equals(input)) { formatter = StrictISODateTimeFormat.time(); } else if ("strictTimeNoMillis".equals(input) || "strict_time_no_millis".equals(input)) { formatter = StrictISODateTimeFormat.timeNoMillis(); } else if ("strictTTime".equals(input) || "strict_t_time".equals(input)) { formatter = StrictISODateTimeFormat.tTime(); } else if ("strictTTimeNoMillis".equals(input) || "strict_t_time_no_millis".equals(input)) { formatter = StrictISODateTimeFormat.tTimeNoMillis(); } else if ("strictWeekDate".equals(input) || "strict_week_date".equals(input)) { formatter = StrictISODateTimeFormat.weekDate(); } else if ("strictWeekDateTime".equals(input) || "strict_week_date_time".equals(input)) { formatter = StrictISODateTimeFormat.weekDateTime(); } else if ("strictWeekDateTimeNoMillis".equals(input) || "strict_week_date_time_no_millis".equals(input)) { formatter = StrictISODateTimeFormat.weekDateTimeNoMillis(); } else if ("strictWeekyear".equals(input) || "strict_weekyear".equals(input)) { formatter = StrictISODateTimeFormat.weekyear(); } else if ("strictWeekyearWeek".equals(input) || "strict_weekyear_week".equals(input)) { formatter = StrictISODateTimeFormat.weekyearWeek(); } else if ("strictWeekyearWeekDay".equals(input) || "strict_weekyear_week_day".equals(input)) { formatter = StrictISODateTimeFormat.weekyearWeekDay(); } else if ("strictYear".equals(input) || "strict_year".equals(input)) { formatter = StrictISODateTimeFormat.year(); } else if ("strictYearMonth".equals(input) || "strict_year_month".equals(input)) { formatter = StrictISODateTimeFormat.yearMonth(); } else if ("strictYearMonthDay".equals(input) || "strict_year_month_day".equals(input)) { formatter = StrictISODateTimeFormat.yearMonthDay(); } else if (Strings.hasLength(input) && input.contains("||")) { String[] formats = Strings.delimitedListToStringArray(input, "||"); DateTimeParser[] parsers = new DateTimeParser[formats.length]; if (formats.length == 1) { formatter = forPattern(input, locale).parser(); } else { DateTimeFormatter dateTimeFormatter = null; for (int i = 0; i < formats.length; i++) { FormatDateTimeFormatter currentFormatter = forPattern(formats[i], locale); DateTimeFormatter currentParser = currentFormatter.parser(); if (dateTimeFormatter == null) { dateTimeFormatter = currentFormatter.printer(); } parsers[i] = currentParser.getParser(); } DateTimeFormatterBuilder builder = new DateTimeFormatterBuilder() .append(dateTimeFormatter.withZone(DateTimeZone.UTC).getPrinter(), parsers); formatter = builder.toFormatter(); } } else { try { formatter = DateTimeFormat.forPattern(input); } catch (IllegalArgumentException e) { throw new IllegalArgumentException("Invalid format: [" + input + "]: " + e.getMessage(), e); } } return new FormatDateTimeFormatter(input, formatter.withZone(DateTimeZone.UTC), locale); }
From source file:org.codelibs.elasticsearch.common.joda.Joda.java
License:Apache License
public static FormatDateTimeFormatter getStrictStandardDateFormatter() { // 2014/10/10 DateTimeFormatter shortFormatter = new DateTimeFormatterBuilder() .appendFixedDecimal(DateTimeFieldType.year(), 4).appendLiteral('/') .appendFixedDecimal(DateTimeFieldType.monthOfYear(), 2).appendLiteral('/') .appendFixedDecimal(DateTimeFieldType.dayOfMonth(), 2).toFormatter().withZoneUTC(); // 2014/10/10 12:12:12 DateTimeFormatter longFormatter = new DateTimeFormatterBuilder() .appendFixedDecimal(DateTimeFieldType.year(), 4).appendLiteral('/') .appendFixedDecimal(DateTimeFieldType.monthOfYear(), 2).appendLiteral('/') .appendFixedDecimal(DateTimeFieldType.dayOfMonth(), 2).appendLiteral(' ') .appendFixedSignedDecimal(DateTimeFieldType.hourOfDay(), 2).appendLiteral(':') .appendFixedSignedDecimal(DateTimeFieldType.minuteOfHour(), 2).appendLiteral(':') .appendFixedSignedDecimal(DateTimeFieldType.secondOfMinute(), 2).toFormatter().withZoneUTC(); DateTimeFormatterBuilder builder = new DateTimeFormatterBuilder() .append(longFormatter.withZone(DateTimeZone.UTC).getPrinter(), new DateTimeParser[] { longFormatter.getParser(), shortFormatter.getParser(), new EpochTimeParser(true) }); return new FormatDateTimeFormatter("yyyy/MM/dd HH:mm:ss||yyyy/MM/dd||epoch_millis", builder.toFormatter().withZone(DateTimeZone.UTC), Locale.ROOT); }
From source file:org.ddialliance.ddiftp.util.Translator.java
License:Open Source License
/** * Format an ISO8601 time string defined by into a Calendar<BR> * Defined by '-'? yyyy '-' mm '-' dd 'T' hh ':' mm ':' ss ('.' s+)? * (zzzzzz)?/* w w w .j a v a 2s.c o m*/ * * @see http://www.w3.org/TR/xmlschema-2/#dateTime * @param time * string * @return calendar * @throws DDIFtpException */ public static Calendar formatIso8601DateTime(String time) throws DDIFtpException { // yyyy-MM-dd'T'HH:mm:ss.SSSZZ full format DateTimeFormatter fmt = ISODateTimeFormat.dateTime(); try { DateTime dateTime = fmt.parseDateTime(time); return dateTime.toCalendar(getLocale()); } catch (IllegalArgumentException e) { try { // yyyy-MM-dd'T'HH:mm:ssZZ with out millisecond fmt = ISODateTimeFormat.dateTimeNoMillis(); fmt.withLocale(getLocale()); fmt.withZone(DateTimeZone.forTimeZone(getTimeZone())); DateTime dateTime = fmt.parseDateTime(time); return dateTime.toCalendar(getLocale()); } catch (IllegalArgumentException e1) { try { // yyyy-MM-dd'T'HH:mm:ss.SS with out time zone fmt = ISODateTimeFormat.dateHourMinuteSecondFraction(); fmt.withLocale(getLocale()); fmt.withZone(DateTimeZone.forTimeZone(getTimeZone())); DateTime dateTime = fmt.parseDateTime(time); return dateTime.toCalendar(getLocale()); } catch (Exception e2) { try { // yyyy-MM-dd'T'HH:mm:ss with out millisecond and time // zone fmt = ISODateTimeFormat.dateHourMinuteSecond(); fmt.withLocale(getLocale()); fmt.withZone(DateTimeZone.forTimeZone(getTimeZone())); DateTime dateTime = fmt.parseDateTime(time); return dateTime.toCalendar(getLocale()); } catch (IllegalArgumentException e3) { try { fmt = ISODateTimeFormat.dateParser(); fmt.withLocale(getLocale()); fmt.withZone(DateTimeZone.forTimeZone(getTimeZone())); DateTime dateTime = fmt.parseDateTime(time); return dateTime.toCalendar(getLocale()); } catch (Exception e4) { throw new DDIFtpException("translate.timeformat.error", new Object[] { time, "'-'? yyyy '-' mm '-' dd 'T' hh ':' mm ':' ss ('.' s+)? (zzzzzz)?" }, e); } } } } } }