@Override public java.sql.Date read(JsonReader in) throws IOException { switch (in.peek()) { case NULL: in.nextNull(); return null; default: String date = in.nextString(); try { if (dateFormat != null) { return new java.sql.Date(dateFormat.parse(date).getTime()); } return new java.sql.Date(ISO8601Utils.parse(date, new ParsePosition(0)).getTime()); } catch (ParseException e) { throw new JsonParseException(e); } } }
@Nullable public static List<Tags> getTagsUpdate(ApiService api, Date updateAtStart, Date updateAtEnd) { List<Tags> list = new ArrayList<>(); int pageSize = 100; String range = ISO8601Utils.format(updateAtStart) + "," + ISO8601Utils.format(updateAtEnd); try { Response<List<Tags>> response = api.getTags(range, pageSize, Pagination.getPage(list, pageSize)).execute(); if (!Tools.apiIsSuccessfulNoThrow(response)) return null; int total = Integer.decode(response.headers().get("X-Total")); list.addAll(response.body()); while (list.size() < total) { response = api.getTags(range, pageSize, Pagination.getPage(list, pageSize)).execute(); if (!Tools.apiIsSuccessfulNoThrow(response)) return null; list.addAll(response.body()); } } catch (IOException e) { e.printStackTrace(); } return list; }
@Override public Date read(JsonReader in) throws IOException { final JsonToken peek = in.peek(); if (peek == JsonToken.NULL) { in.nextNull(); return null; } else { String stringValue = in.nextString(); if (!stringValue.isEmpty()) { try { return ISO8601Utils.parse(stringValue, new ParsePosition(0)); } catch (ParseException e) { try { return getDateFormat().parse(stringValue); } catch (ParseException f) { throw new JsonSyntaxException(stringValue, e); } } } else { return null; } } }
public static Date convertToDate(String date) { if(date==null){ return null; } java.util.Date parsedate = null; final ParsePosition parsePosition = new ParsePosition(0); try { parsedate = ISO8601Utils.parse(date, parsePosition); logger.debug("Parsed Data"+parsedate); return parsedate; } catch (ParseException e) { logger.error(e); } return parsedate; }
@Override protected int getUpdatedAt(D d) { try { Customer c = (Customer) d; return (int) (ISO8601Utils.parse(c.getUpdatedAt(), new ParsePosition(0)).getTime() / 1000); } catch (ParseException e) { e.printStackTrace(); } return 0; }
@Override public void write(JsonWriter out, Date date) throws IOException { if (date == null) { out.nullValue(); } else { String value; if (dateFormat != null) { value = dateFormat.format(date); } else { value = ISO8601Utils.format(date, true); } out.value(value); } }
@Override @NonNull public Date read(final JsonReader in) throws IOException { final String date = in.nextString(); final ParsePosition parsePosition = new ParsePosition(0); try { return ISO8601Utils.parse(date, parsePosition); } catch (ParseException e) { throw new JsonSyntaxException(date, e); } }
/** * Post the provided data by passing the appropriate headers and status code in a request chain * through an {@link NewVersionBroadcastInterceptor}. * * @param interceptor The interceptor through which the data is to be posted. * @param newVersion The new version. * @param lastSupportedDate The last supported date. * @param isUnsupported Whether the current version is unsupported. * @return The posted event. This can be null if the data does not constitute a valid event. */ @Nullable private static NewVersionAvailableEvent postEventFromInterceptor( @NonNull final NewVersionBroadcastInterceptor interceptor, @Nullable final Version newVersion, @Nullable final Date lastSupportedDate, final boolean isUnsupported) throws IOException { // This will throw an AssumptionViolatedException if the Android runtime // isn't loaded (i.e. through using the Robolectric test runner). final EventBus eventBus = getEventBus(); eventBus.removeStickyEvent(NewVersionAvailableEvent.class); final Interceptor.Chain chain = mock(Interceptor.Chain.class); final Request request = new Request.Builder() .url("https://localhost:1/") .build(); final Response response; { final Response.Builder responseBuilder = new Response.Builder(); responseBuilder.request(request); responseBuilder.protocol(Protocol.HTTP_1_1); responseBuilder.code(isUnsupported ? UPGRADE_REQUIRED : ACCEPTED); if (newVersion != null) { responseBuilder.header(HEADER_APP_LATEST_VERSION, newVersion.toString()); } if (lastSupportedDate != null) { responseBuilder.header(HEADER_APP_VERSION_LAST_SUPPORTED_DATE, ISO8601Utils.format(lastSupportedDate, true)); } response = responseBuilder.build(); } when(chain.request()).thenReturn(request); when(chain.proceed(request)).thenReturn(response); interceptor.intercept(chain); final InOrder inOrder = inOrder(chain); inOrder.verify(chain).request(); inOrder.verify(chain).proceed(request); verifyNoMoreInteractions(chain); return eventBus.getStickyEvent(NewVersionAvailableEvent.class); }
@Test public void testGetCurrentTimeStamp() { String date = DateUtil.getCurrentTimeStamp(); assertNotNull(date); print("modification date = " + date); // Verify that it's been formatted to an ISO 8601 // compatible format. try { ISO8601Utils.parse(date, new ParsePosition(0)); } catch (ParseException e) { fail("Invalid date format: '" + date + "' is not an ISO 8601 compliant string", e); } }
static public Gson getGson() { class DateDeserializer implements JsonDeserializer<Date> { private final String TAG = DateDeserializer.class.getSimpleName(); @Override public java.util.Date deserialize(JsonElement element, Type arg1, JsonDeserializationContext arg2) throws JsonParseException { String date = element.getAsString(); Date returnDate = null; try { returnDate = ISO8601Utils.parse(date, new ParsePosition(0)); } catch (ParseException | IllegalArgumentException e) { Log.e(TAG, "Failed to parse dateString: (" + date + "), due to:", e); } return returnDate; } } class DateSerializer implements JsonSerializer<Date> { @Override public JsonElement serialize(java.util.Date src, Type typeOfSrc, JsonSerializationContext context) { String dateFormatAsString = ISO8601Utils.format(src, false, TimeZone.getTimeZone("GMT")); return new JsonPrimitive(dateFormatAsString); } } return new GsonBuilder() .setDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'") .registerTypeAdapter(Slots.class, new Slots.SlotsDeserializer()) .registerTypeAdapter(Slots.class, new Slots.SlotsSerializer()) .registerTypeAdapter(java.util.Date.class, new DateDeserializer()) .registerTypeAdapter(java.util.Date.class, new DateSerializer()) .registerTypeAdapter(UsersLTE.class, new UsersLTE.UserLTEDeserializer()) .registerTypeAdapter(Messages.UserVotes.class, new Messages.UserVotes.UserVotesDeserializer()) .registerTypeAdapter(UsersLTE.getListType(), new UsersLTE.ListUserLTEDeserializer()) // .registerTypeAdapter(ScaleTeams.class, new ScaleTeams.ScaleTeamsDeserializer()) .create(); }
@Override public void write(@NonNull JsonWriter out, @NonNull Date date) throws IOException { out.value(ISO8601Utils.format(date, true)); }
/** * @return The current date and time in a ISO 8601 compliant format. */ public static String getCurrentTimeStamp(){ return ISO8601Utils.format(new Date(), true); // Find todays date }
/** * Formats date time to ISO 8601 string. * * @param dateTime date time to format * @return formatted string */ public static String format(DateTime dateTime) { Calendar calendar = checkNotNull(dateTime, "dateTime").getCalendar(); return ISO8601Utils.format(calendar.getTime(), true, calendar.getTimeZone()); }