List of usage examples for org.joda.time.format DateTimeFormat forPattern
public static DateTimeFormatter forPattern(String pattern)
From source file:com.example.weatherforecast.service.YahooWeatherApiResponse.java
License:Open Source License
private WeatherData forecastJsonItemToWeatherData(JsonNode jsonNode) { LocalDate date = JsonUtils.localDateFromJsonNode(jsonNode, DATE_NODE, DateTimeFormat.forPattern("dd MMM yyyy")); Integer tempLow = JsonUtils.integerFromJsonNode(jsonNode, TEMP_LOW_NODE); Integer tempHigh = JsonUtils.integerFromJsonNode(jsonNode, TEMP_HIGH_NODE); return new WeatherData(date, tempLow, tempHigh); }
From source file:com.facebook.presto.operator.scalar.DateTimeFunctions.java
License:Apache License
@Description("parses the specified date/time by the given format") @ScalarFunction//from w w w . j a v a 2s . c om @SqlType(TimestampWithTimeZoneType.class) public static long parseDatetime(ConnectorSession session, @SqlType(VarcharType.class) Slice datetime, @SqlType(VarcharType.class) Slice formatString) { String pattern = formatString.toString(Charsets.UTF_8); DateTimeFormatter formatter = DateTimeFormat.forPattern(pattern) .withChronology(getChronology(session.getTimeZoneKey())).withOffsetParsed() .withLocale(session.getLocale()); String datetimeString = datetime.toString(Charsets.UTF_8); return DateTimeZoneIndex.packDateTimeWithZone(parseDateTimeHelper(formatter, datetimeString)); }
From source file:com.facebook.presto.operator.scalar.DateTimeFunctions.java
License:Apache License
private static Slice formatDatetime(ISOChronology chronology, Locale locale, long timestamp, Slice formatString) {//w ww. j a v a2 s . co m String pattern = formatString.toString(Charsets.UTF_8); DateTimeFormatter formatter = DateTimeFormat.forPattern(pattern).withChronology(chronology) .withLocale(locale); String datetimeString = formatter.print(timestamp); return Slices.wrappedBuffer(datetimeString.getBytes(Charsets.UTF_8)); }
From source file:com.facebook.presto.operator.scalar.UnixTimeFunctions.java
License:Apache License
@Description("parses the specified date/time by the given format") @ScalarFunction/*from w w w.jav a2 s. c om*/ 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
@Description("formats the given time by the given format") @ScalarFunction//w w w . j a v a2s . c o m public static Slice formatDatetime(long unixTime, Slice formatString) { String pattern = formatString.toString(Charsets.UTF_8); DateTimeFormatter formatter = DateTimeFormat.forPattern(pattern).withZoneUTC(); String datetimeString = formatter.print(toMillis(unixTime)); return Slices.wrappedBuffer(datetimeString.getBytes(Charsets.UTF_8)); }
From source file:com.fatboyindustrial.gsonjodatime.LocalDateConverter.java
License:Open Source License
/** * Gson invokes this call-back method during serialization when it encounters a field of the * specified type. <p>/*from w ww . j a v a2 s . c o m*/ * * In the implementation of this call-back method, you should consider invoking * {@link com.google.gson.JsonSerializationContext#serialize(Object, java.lang.reflect.Type)} method to create JsonElements for any * non-trivial field of the {@code src} object. However, you should never invoke it on the * {@code src} object itself since that will cause an infinite loop (Gson will call your * call-back method again). * * @param src the object that needs to be converted to Json. * @param typeOfSrc the actual type (fully genericized version) of the source object. * @return a JsonElement corresponding to the specified object. */ @Override public JsonElement serialize(LocalDate src, Type typeOfSrc, JsonSerializationContext context) { final DateTimeFormatter fmt = DateTimeFormat.forPattern(PATTERN); return new JsonPrimitive(fmt.print(src)); }
From source file:com.fatboyindustrial.gsonjodatime.LocalDateConverter.java
License:Open Source License
/** * Gson invokes this call-back method during deserialization when it encounters a field of the * specified type. <p>/*from w w w . j a va2 s.co m*/ * * In the implementation of this call-back method, you should consider invoking * {@link com.google.gson.JsonDeserializationContext#deserialize(com.google.gson.JsonElement, java.lang.reflect.Type)} method to create objects * for any non-trivial field of the returned object. However, you should never invoke it on the * the same type passing {@code json} since that will cause an infinite loop (Gson will call your * call-back method again). * * @param json The Json data being deserialized * @param typeOfT The type of the Object to deserialize to * @return a deserialized object of the specified type typeOfT which is a subclass of {@code T} * @throws com.google.gson.JsonParseException if json is not in the expected format of {@code typeOfT} */ @Override public LocalDate deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { final DateTimeFormatter fmt = DateTimeFormat.forPattern(PATTERN); return fmt.parseLocalDate(json.getAsString()); }
From source file:com.fatboyindustrial.gsonjodatime.LocalDateTimeConverter.java
License:Open Source License
/** * Gson invokes this call-back method during serialization when it encounters a field of the * specified type. <p>/* w w w . ja va 2 s .c o m*/ * * In the implementation of this call-back method, you should consider invoking * {@link JsonSerializationContext#serialize(Object, Type)} method to create JsonElements for any * non-trivial field of the {@code src} object. However, you should never invoke it on the * {@code src} object itself since that will cause an infinite loop (Gson will call your * call-back method again). * * @param src the object that needs to be converted to Json. * @param typeOfSrc the actual type (fully genericized version) of the source object. * @return a JsonElement corresponding to the specified object. */ @Override public JsonElement serialize(LocalDateTime src, Type typeOfSrc, JsonSerializationContext context) { final DateTimeFormatter fmt = DateTimeFormat.forPattern(PATTERN); return new JsonPrimitive(fmt.print(src)); }
From source file:com.fatboyindustrial.gsonjodatime.LocalDateTimeConverter.java
License:Open Source License
/** * Gson invokes this call-back method during deserialization when it encounters a field of the * specified type. <p>/*from w ww .j av a2 s . c o m*/ * * In the implementation of this call-back method, you should consider invoking * {@link JsonDeserializationContext#deserialize(JsonElement, Type)} method to create objects * for any non-trivial field of the returned object. However, you should never invoke it on the * the same type passing {@code json} since that will cause an infinite loop (Gson will call your * call-back method again). * * @param json The Json data being deserialized * @param typeOfT The type of the Object to deserialize to * @return a deserialized object of the specified type typeOfT which is a subclass of {@code T} * @throws JsonParseException if json is not in the expected format of {@code typeOfT} */ @Override public LocalDateTime deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { final DateTimeFormatter fmt = DateTimeFormat.forPattern(PATTERN); return fmt.parseLocalDateTime(json.getAsString()); }
From source file:com.fatboyindustrial.gsonjodatime.LocalTimeConverter.java
License:Open Source License
/** * Gson invokes this call-back method during serialization when it encounters a field of the * specified type. <p>/*from w w w .ja v a 2 s . com*/ * * In the implementation of this call-back method, you should consider invoking * {@link JsonSerializationContext#serialize(Object, Type)} method to create JsonElements for any * non-trivial field of the {@code src} object. However, you should never invoke it on the * {@code src} object itself since that will cause an infinite loop (Gson will call your * call-back method again). * * @param src the object that needs to be converted to Json. * @param typeOfSrc the actual type (fully genericized version) of the source object. * @return a JsonElement corresponding to the specified object. */ @Override public JsonElement serialize(LocalTime src, Type typeOfSrc, JsonSerializationContext context) { final DateTimeFormatter fmt = DateTimeFormat.forPattern(PATTERN); return new JsonPrimitive(fmt.print(src)); }