List of usage examples for java.time OffsetDateTime parse
public static OffsetDateTime parse(CharSequence text, DateTimeFormatter formatter)
From source file:Main.java
public static void main(String[] args) { OffsetDateTime o = OffsetDateTime.parse("2014-09-02T15:22:14.59-07:00", DateTimeFormatter.ISO_DATE_TIME); System.out.println(o);// ww w. j a va2 s .c om }
From source file:webby.commons.io.jackson.OffsetDateTimeDeserializer.java
@Override public OffsetDateTime deserialize(JsonParser parser, DeserializationContext context) throws IOException { switch (parser.getCurrentToken()) { case VALUE_STRING: String string = parser.getText().trim(); if (prepareFn != null) string = prepareFn.apply(string); if (string == null || string.length() == 0) return null; return OffsetDateTime.parse(string, formatter); }/*from ww w. j a v a 2 s.c o m*/ throw context.wrongTokenException(parser, JsonToken.START_ARRAY, "Expected array or string."); }
From source file:Main.StaticTools.java
public static ZonedDateTime getZonedTimeFromStr(String input) { ZonedDateTime result = null;/*www .j av a2 s .com*/ if (input != null) try { result = OffsetDateTime.parse(input, ExifDateFormatTZ).toZonedDateTime(); } catch (DateTimeParseException e) { try { result = OffsetDateTime.parse(input, XmpDateFormatTZ).toZonedDateTime(); } catch (DateTimeParseException e1) { } } return result; }
From source file:fi.hsl.parkandride.itest.PredictionITest.java
@Test public void prediction_JSON_structure() { Utilization u = makeDummyPredictions(); JsonPath json = when().get(UrlSchema.FACILITY_PREDICTION, facilityId).jsonPath(); long facilityId = json.getLong("[0].facilityId"); String capacityType = json.getString("[0].capacityType"); String usage = json.getString("[0].usage"); OffsetDateTime timestamp = OffsetDateTime.parse(json.getString("[0].timestamp"), DateTimeFormatter.ISO_OFFSET_DATE_TIME); int spacesAvailable = json.getInt("[0].spacesAvailable"); assertThat(facilityId).as("facilityId").isEqualTo(u.facilityId); assertThat(capacityType).as("capacityType").isEqualTo(u.capacityType.name()); assertThat(usage).as("usage").isEqualTo(u.usage.name()); assertThat(timestamp.getOffset()).as("time should be in local timezone") .isEqualTo(ZoneOffset.systemDefault().getRules().getOffset(timestamp.toInstant())); assertThat(spacesAvailable).as("spacesAvailable").isEqualTo(u.spacesAvailable); }
From source file:org.openhab.binding.dwdunwetter.internal.data.DwdWarningsData.java
private Instant getTimestampValue(XMLEventReader eventReader) throws XMLStreamException { XMLEvent event = eventReader.nextEvent(); String dateTimeString = event.asCharacters().getData(); try {//from ww w .j a va 2 s .co m OffsetDateTime dateTime = OffsetDateTime.parse(dateTimeString, formatter); return dateTime.toInstant(); } catch (DateTimeParseException e) { logger.debug("Exception while parsing a DateTime", e); return Instant.MIN; } }
From source file:org.onosproject.drivers.polatis.netconf.PolatisAlarmConsumer.java
private long getTimeRaised(HierarchicalConfiguration cfg) { long timeRaised; String alarmTime = cfg.getString(ALARM_TIME); try {//from w w w . j a va2 s.co m OffsetDateTime date = OffsetDateTime.parse(alarmTime, DateTimeFormatter.ISO_OFFSET_DATE_TIME); timeRaised = date.toInstant().toEpochMilli(); return timeRaised; } catch (DateTimeException e) { log.error("Cannot parse exception {} {}", alarmTime, e); } return System.currentTimeMillis(); }
From source file:net.dv8tion.jda.core.requests.ratelimit.BotRateLimiter.java
private void setTimeOffset(Headers headers) { //Store as soon as possible to get the most accurate time difference; long time = System.currentTimeMillis(); if (timeOffset == null) { //Get the date header provided by Discord. //Format: "date" : "Fri, 16 Sep 2016 05:49:36 GMT" String date = headers.get("Date"); if (date != null) { OffsetDateTime tDate = OffsetDateTime.parse(date, DateTimeFormatter.RFC_1123_DATE_TIME); long lDate = tDate.toInstant().toEpochMilli(); //We want to work in milliseconds, not seconds timeOffset = lDate - time; //Get offset in milliseconds. }//from ww w . j a va 2s . c om } }
From source file:com.amazonaws.services.kinesis.io.StringDataExtractor.java
/** * {@inheritDoc}/*from w w w.j av a2 s.c o m*/ */ @Override public List<AggregateData> getData(InputEvent event) throws SerializationException { try { int summaryIndex = -1; String dateString; OffsetDateTime dateValue; List<AggregateData> data = new ArrayList<>(); List<List<String>> content = serialiser.toClass(event); for (List<String> line : content) { if (line != null) { LabelSet labels = new LabelSet(); labels.withAlias(this.labelAttributeAlias); for (Integer key : this.labelIndicies) { labels.put("" + key, line.get(key)); } // get the unique index String uniqueId = null; if (this.usePartitionKeyForUnique) { uniqueId = event.getPartitionKey(); } else if (this.useSequenceForUnique) { uniqueId = event.getSequenceNumber(); } else { if (this.uniqueIdIndex != -1) { uniqueId = line.get(this.uniqueIdIndex); } } // get the date value from the line if (this.dateValueIndex != -1) { dateString = line.get(dateValueIndex); if (this.dateFormat != null) { dateValue = OffsetDateTime.parse(dateString, dateFormatter); } else { // no formatter, so treat as epoch seconds try { dateValue = OffsetDateTime.ofInstant( Instant.ofEpochMilli(Long.parseLong(dateString)), ZoneId.of("UTC")); } catch (Exception e) { LOG.error(String.format( "Unable to create Date Value element from item '%s' due to invalid format as Epoch Seconds", dateValueIndex)); throw new SerializationException(e); } } } else { dateValue = OffsetDateTime.now(ZoneId.of("UTC")); } // get the summed values if (this.aggregatorType.equals(AggregatorType.SUM)) { sumUpdates = new HashMap<>(); // get the positional sum items for (int i = 0; i < summaryIndicies.size(); i++) { summaryIndex = summaryIndicies.get(i); try { sumUpdates.put("" + summaryIndex, Double.parseDouble(line.get(summaryIndex))); } catch (NumberFormatException nfe) { LOG.error(String.format( "Unable to deserialise Summary '%s' due to NumberFormatException", i)); throw new SerializationException(nfe); } } } data.add(new AggregateData(uniqueId, labels, dateValue, sumUpdates)); } } return data; } catch (Exception e) { throw new SerializationException(e); } }
From source file:com.amazonaws.services.kinesis.io.JsonDataExtractor.java
/** * {@inheritDoc}//from w w w . j av a2 s. c om */ @Override public List<AggregateData> getData(InputEvent event) throws SerializationException { try { List<AggregateData> aggregateData = new ArrayList<>(); OffsetDateTime dateValue = null; JsonNode jsonContent = null; String dateString, summary = null; long localOffset = 0; List<String> items = (List<String>) serialiser.toClass(event); // log a warning if we didn't get anything back from the serialiser // - this could be OK, but probably isn't // it would be OK, for example, if you have filterRegex // if (items == null || items.size() == 0) // LOG.warn(String.format( // "Failed to deserialise any content for Record (Partition Key %s, Sequence %s", // event.getPartitionKey(), event.getSequenceNumber())); // process all the items returned by the serialiser for (String item : items) { // Convert the string to a Jackson JsonNode for navigation jsonContent = StreamAggregatorUtils.asJsonNode(item); sumUpdates = new HashMap<>(); LabelSet labels = new LabelSet(); for (String key : this.labelAttributes) { labels.put(key, StreamAggregatorUtils.readValueAsString(jsonContent, key)); } // get the unique ID for the event String uniqueId = null; if (this.uniqueIdAttribute != null) { switch (this.uniqueIdAttribute) { case StreamAggregator.REF_PARTITION_KEY: uniqueId = event.getPartitionKey(); break; case StreamAggregator.REF_SEQUENCE: uniqueId = event.getSequenceNumber(); break; default: uniqueId = StreamAggregatorUtils.readValueAsString(jsonContent, uniqueIdAttribute); break; } } // get the date value from the line if (dateValueAttribute != null) { dateString = StreamAggregatorUtils.readValueAsString(jsonContent, dateValueAttribute); // bail on no date returned if (dateString == null || dateString.equals("")) throw new SerializationException( String.format("Unable to read date value attribute %s from JSON Content %s", dateValueAttribute, item)); // turn date as long or string into Date if (this.dateFormat != null) { dateValue = OffsetDateTime.parse(dateString, dateFormatter); } else { // no formatter, so treat as epoch seconds try { dateValue = OffsetDateTime.ofInstant(Instant.ofEpochMilli(Long.parseLong(dateString)), ZoneId.of("UTC")); } catch (Exception e) { LOG.error(String.format( "Unable to create Date Value element from item '%s' due to invalid format as Epoch Seconds", dateValueAttribute)); throw new SerializationException(e); } } } else { // no date value attribute configured, so use now dateValue = OffsetDateTime.now(ZoneId.of("UTC")); } // get the summed values if (this.aggregatorType.equals(AggregatorType.SUM)) { // get the positional sum items for (String s : summaryConfig.getItemSet()) { try { summary = StreamAggregatorUtils.readValueAsString(jsonContent, s); // if a summary is not found in the data element, // then we simply continue without it if (summary != null) sumUpdates.put(s, Double.parseDouble(summary)); } catch (NumberFormatException nfe) { LOG.error(String .format("Unable to deserialise Summary '%s' due to NumberFormatException", s)); throw new SerializationException(nfe); } } } // get local offset if (offsetAttribute != null) { try { String offsetString = StreamAggregatorUtils.readValueAsString(jsonContent, offsetAttribute); if (offsetString != null) localOffset = Long.parseLong(offsetString); } catch (NumberFormatException nfe) { LOG.error(String.format( "Unable to deserialise local offset '%s' due to NumberFormatException", dateValueAttribute)); throw new SerializationException(nfe); } } aggregateData.add(new AggregateData(uniqueId, labels, dateValue, sumUpdates, localOffset)); } return aggregateData; } catch (Exception e) { throw new SerializationException(e); } }