Example usage for java.time Instant parse

List of usage examples for java.time Instant parse

Introduction

In this page you can find the example usage for java.time Instant parse.

Prototype

public static Instant parse(final CharSequence text) 

Source Link

Document

Obtains an instance of Instant from a text string such as 2007-12-03T10:15:30.00Z .

Usage

From source file:org.apache.tinkerpop.gremlin.server.GremlinServerHttpIntegrateTest.java

@Test
public void should200OnPOSTWithGremlinJsonEndcodedBodyForJavaTime() throws Exception {
    // basic test of java.time.* serialization over JSON from the server perspective. more complete tests
    // exist in gremlin-core
    final CloseableHttpClient httpclient = HttpClients.createDefault();
    final HttpPost httppost = new HttpPost(TestClientFactory.createURLString());
    httppost.addHeader("Content-Type", "application/json");
    httppost.setEntity(new StringEntity("{\"gremlin\":\"java.time.Instant.MAX\"}", Consts.UTF_8));

    try (final CloseableHttpResponse response = httpclient.execute(httppost)) {
        assertEquals(200, response.getStatusLine().getStatusCode());
        assertEquals("application/json", response.getEntity().getContentType().getValue());
        final String json = EntityUtils.toString(response.getEntity());
        final JsonNode node = mapper.readTree(json);
        assertEquals(Instant.MAX, Instant.parse(node.get("result").get("data").get(GraphSONTokens.VALUEPROP)
                .get(0).get(GraphSONTokens.VALUEPROP).asText()));
    }//  w  w  w . j  a  v a2 s  . c  om
}

From source file:com.orange.ngsi2.client.Ngsi2ClientTest.java

@Test
public void testAddSubscription_OK() throws Exception {

    HttpHeaders responseHeader = new HttpHeaders();
    responseHeader.add("Location", "/v2/subscriptions/abcde98765");

    mockServer.expect(requestTo(baseURL + "/v2/subscriptions")).andExpect(method(HttpMethod.POST))
            .andExpect(header("Content-Type", MediaType.APPLICATION_JSON_VALUE))
            .andExpect(jsonPath("$.subject.entities[0].type").value("Room"))
            .andExpect(jsonPath("$.subject.condition.attributes[0]").value("temperature"))
            .andExpect(jsonPath("$.subject.condition.expression.q").value("temperature>40"))
            .andRespond(withNoContent().headers(responseHeader));

    SubjectEntity subjectEntity = new SubjectEntity();
    subjectEntity.setType(Optional.of("Room"));
    Condition condition = new Condition();
    condition.setAttributes(Collections.singletonList("temperature"));
    condition.setExpression("q", "temperature>40");
    SubjectSubscription subjectSubscription = new SubjectSubscription(Collections.singletonList(subjectEntity),
            condition);//from w  ww .jav a 2 s  . co  m
    List<String> attributes = new ArrayList<>();
    attributes.add("temperature");
    attributes.add("humidity");
    Notification notification = new Notification(attributes, new URL("http://localhost:1234"));
    notification.setThrottling(Optional.of(new Long(5)));
    Subscription subscription = new Subscription();
    subscription.setSubject(subjectSubscription);
    subscription.setNotification(notification);
    subscription.setExpires(Instant.parse("2016-04-05T14:00:00.20Z"));

    assertEquals("abcde98765", ngsiClient.addSubscription(subscription).get());
}

From source file:org.codice.ddf.catalog.ui.util.EndpointUtil.java

public Instant parseDate(Serializable value) {

    if (value instanceof Date) {
        return ((Date) value).toInstant();
    }/*from  w w w .jav  a 2s.c o m*/

    if (value instanceof Long) {
        return Instant.ofEpochMilli((Long) value);
    }

    if (!(value instanceof String)) {
        return null;
    }

    String string = String.valueOf(value);

    if (StringUtils.isBlank(string)) {
        return null;
    }

    if (StringUtils.isNumeric(string)) {
        try {
            return Instant.ofEpochMilli(Long.parseLong(string));
        } catch (NumberFormatException ex) {
            LOGGER.debug("Failed to create Epoch time from a numeric: {}", string, ex);
            return null;
        }
    }

    if (iso8601Z.matcher(string).matches()) {
        return Instant.parse(string);
    }

    if (iso8601Offset.matcher(string).matches()) {
        return OffsetDateTime.parse(string).toInstant();
    }

    SimpleDateFormat dateFormat;
    if (jsonDefault.matcher(string).matches()) {
        dateFormat = new SimpleDateFormat("EEE MMM d HH:mm:ss zzz yyyy");
    } else {
        dateFormat = new SimpleDateFormat();
    }
    dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));

    try {
        return dateFormat.parse(string).toInstant();
    } catch (ParseException e) {
        LOGGER.debug("Failed to parse as a dateFormat time from a date string: {}", string, e);
        return null;
    }
}

From source file:com.orange.ngsi2.client.Ngsi2ClientTest.java

@Test
public void testAddSubscription_returnEmpty() throws Exception {

    HttpHeaders responseHeader = new HttpHeaders();
    responseHeader.add("Location", "");

    mockServer.expect(requestTo(baseURL + "/v2/subscriptions")).andExpect(method(HttpMethod.POST))
            .andExpect(header("Content-Type", MediaType.APPLICATION_JSON_VALUE))
            .andExpect(jsonPath("$.subject.entities[0].type").value("Room"))
            .andExpect(jsonPath("$.subject.condition.attributes[0]").value("temperature"))
            .andExpect(jsonPath("$.subject.condition.expression.q").value("temperature>40"))
            .andRespond(withNoContent().headers(responseHeader));

    SubjectEntity subjectEntity = new SubjectEntity();
    subjectEntity.setType(Optional.of("Room"));
    Condition condition = new Condition();
    condition.setAttributes(Collections.singletonList("temperature"));
    condition.setExpression("q", "temperature>40");
    SubjectSubscription subjectSubscription = new SubjectSubscription(Collections.singletonList(subjectEntity),
            condition);/*from  ww  w. j a  va 2 s  .  com*/
    List<String> attributes = new ArrayList<>();
    attributes.add("temperature");
    attributes.add("humidity");
    Notification notification = new Notification(attributes, new URL("http://localhost:1234"));
    notification.setThrottling(Optional.of(new Long(5)));
    Subscription subscription = new Subscription();
    subscription.setSubject(subjectSubscription);
    subscription.setNotification(notification);
    subscription.setExpires(Instant.parse("2016-04-05T14:00:00.20Z"));

    assertEquals("", ngsiClient.addSubscription(subscription).get());
}

From source file:com.orange.ngsi2.client.Ngsi2ClientTest.java

@Test
public void testUpdateSubscription_OK() throws Exception {

    mockServer.expect(requestTo(baseURL + "/v2/subscriptions/abcdef")).andExpect(method(HttpMethod.PATCH))
            .andExpect(header("Content-Type", MediaType.APPLICATION_JSON_VALUE))
            .andExpect(jsonPath("$.expires").value("2017-04-05T14:00:00.200Z")).andRespond(withNoContent());

    Subscription subscription = new Subscription();
    subscription.setExpires(Instant.parse("2017-04-05T14:00:00.200Z"));
    ngsiClient.updateSubscription("abcdef", subscription).get();
}

From source file:nl.knaw.huygens.alexandria.service.TinkerPopService.java

private TentativeAlexandriaProvenance deframeProvenance(AlexandriaVF avf) {
    String provenanceWhen = avf.getProvenanceWhen();
    return new TentativeAlexandriaProvenance(avf.getProvenanceWho(), Instant.parse(provenanceWhen),
            avf.getProvenanceWhy());/*from w ww  .  j av  a  2  s. c  o  m*/
}

From source file:org.codice.ddf.rest.impl.CatalogServiceImpl.java

private void parseAttribute(Map<String, AttributeImpl> attributeMap, String parsedName, InputStream inputStream,
        AttributeType.AttributeFormat attributeFormat) {
    try (InputStream is = inputStream;
            InputStream boundedStream = new BoundedInputStream(is, MAX_INPUT_SIZE + 1L)) {
        if (attributeFormat == OBJECT) {
            LOGGER.debug("Object type not supported for override");
            return;
        }//  w w w.j a v a  2 s  .com

        byte[] bytes = IOUtils.toByteArray(boundedStream);
        if (bytes.length > MAX_INPUT_SIZE) {
            LOGGER.debug("Attribute length is limited to {} bytes", MAX_INPUT_SIZE);
            return;
        }

        AttributeImpl attribute;
        if (attributeMap.containsKey(parsedName)) {
            attribute = attributeMap.get(parsedName);
        } else {
            attribute = new AttributeImpl(parsedName, Collections.emptyList());
            attributeMap.put(parsedName, attribute);
        }

        if (attributeFormat == BINARY) {
            attribute.addValue(bytes);
            return;
        }

        String value = new String(bytes, Charset.defaultCharset());

        switch (attributeFormat) {
        case XML:
        case GEOMETRY:
        case STRING:
            attribute.addValue(value);
            break;
        case BOOLEAN:
            attribute.addValue(Boolean.valueOf(value));
            break;
        case SHORT:
            attribute.addValue(Short.valueOf(value));
            break;
        case LONG:
            attribute.addValue(Long.valueOf(value));
            break;
        case INTEGER:
            attribute.addValue(Integer.valueOf(value));
            break;
        case FLOAT:
            attribute.addValue(Float.valueOf(value));
            break;
        case DOUBLE:
            attribute.addValue(Double.valueOf(value));
            break;
        case DATE:
            try {
                Instant instant = Instant.parse(value);
                attribute.addValue(Date.from(instant));
            } catch (DateTimeParseException e) {
                LOGGER.debug("Unable to parse instant '{}'", attribute, e);
            }
            break;
        default:
            LOGGER.debug("Attribute format '{}' not supported", attributeFormat);
            break;
        }
    } catch (IOException e) {
        LOGGER.debug("Unable to read attribute to override", e);
    }
}

From source file:org.sakaiproject.contentreview.turnitin.oc.ContentReviewServiceTurnitinOC.java

@Override
public Instant getEndUserLicenseAgreementTimestamp() {
    Instant validFrom = null;/* ww  w .ja  v  a 2s  . co m*/
    Map<String, Object> latestEula = getLatestEula();
    if (latestEula != null && latestEula.containsKey("valid_from")) {
        try {
            validFrom = Instant.parse(latestEula.get("valid_from").toString());
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
    }
    return validFrom;
}