コード例 #1
0
  public static void main(String[] args) throws ParseException {

    OffsetDateTime start = OffsetDateTime.parse("2014-06-10T10:45:00+03:00");
    // Takes the time stamp and converts to an OffsetDateTime object
    OffsetDateTime end = OffsetDateTime.parse("2015-06-10T10:50:00+03:00");

    Instant startInst = start.toInstant(); // converts it to an Instant Class object
    Instant endInst = end.toInstant();
    Duration dur =
        Duration.between(startInst, endInst); // calculates the duration between the two instances

    System.out.println("The Period between the two instants is:\n");
    System.out.println(dur.toMillis() + " Millisecs"); // prints the milliseconds
  }
コード例 #2
0
 @Override
 protected void writeDateTime(BsonWriter writer, String name, Object value, EncoderContext ctx) {
   writer.writeDateTime(
       OffsetDateTime.parse(((JsonObject) value).getString(DATE_FIELD))
           .toInstant()
           .toEpochMilli());
 }
コード例 #3
0
  // TODO clean up
  @Override
  public void testGoogleFitMeasureFromDataPoint(
      BodyHeight testMeasure, Map<String, Object> properties) {

    BodyHeight.Builder bodyHeightBuilder =
        new BodyHeight.Builder(new LengthUnitValue(METER, (double) properties.get("fpValue")));
    if (properties.containsKey("endDateTimeString")) {
      bodyHeightBuilder.setEffectiveTimeFrame(
          TimeInterval.ofStartDateTimeAndEndDateTime(
              OffsetDateTime.parse((String) properties.get("startDateTimeString")),
              OffsetDateTime.parse((String) properties.get("endDateTimeString"))));
    } else {
      bodyHeightBuilder.setEffectiveTimeFrame(
          OffsetDateTime.parse((String) properties.get("startDateTimeString")));
    }
    BodyHeight expectedBodyHeight = bodyHeightBuilder.build();
    assertThat(testMeasure, equalTo(expectedBodyHeight));
  }
コード例 #4
0
 private static Instant getInstant(String str) {
   try {
     Instant i = OffsetDateTime.parse(str).toInstant();
     LOG.debug(str + " resolved to Instant");
     return i;
   } catch (DateTimeException e) {
     LOG.debug(str + " does not match to OffsetDateTime pattern");
   }
   return null;
 }
コード例 #5
0
  @Override
  public Optional<String> validate(final String dateTime) {
    try {
      OffsetDateTime.parse(dateTime, ISO_OFFSET_DATE_TIME);

      // Unfortunately, ISO_OFFSET_DATE_TIME accepts offsets with seconds, which is not RFC3339
      // compliant. So
      // we need to do some further checks using a regex in order to be sure that it adhere to the
      // given format.
      final Matcher matcher = pattern.matcher(dateTime);
      if (matcher.matches()) {
        return Optional.empty();
      } else {
        return error;
      }

    } catch (final DateTimeParseException e) {
      return error;
    }
  }
コード例 #6
0
 @Override
 public TemporalAccessor parse(String text, Locale locale) throws ParseException {
   DateTimeFormatter formatterToUse = DateTimeContextHolder.getFormatter(this.formatter, locale);
   if (LocalDate.class.equals(this.temporalAccessorType)) {
     return LocalDate.parse(text, formatterToUse);
   } else if (LocalTime.class.equals(this.temporalAccessorType)) {
     return LocalTime.parse(text, formatterToUse);
   } else if (LocalDateTime.class.equals(this.temporalAccessorType)) {
     return LocalDateTime.parse(text, formatterToUse);
   } else if (ZonedDateTime.class.equals(this.temporalAccessorType)) {
     return ZonedDateTime.parse(text, formatterToUse);
   } else if (OffsetDateTime.class.equals(this.temporalAccessorType)) {
     return OffsetDateTime.parse(text, formatterToUse);
   } else if (OffsetTime.class.equals(this.temporalAccessorType)) {
     return OffsetTime.parse(text, formatterToUse);
   } else {
     throw new IllegalStateException(
         "Unsupported TemporalAccessor type: " + this.temporalAccessorType);
   }
 }
コード例 #7
0
  public static void main(String... args) {
    F<String, F<String, Date>> simpleDate =
        df ->
            s -> {
              try {
                return new SimpleDateFormat(df).parse(s);
              } catch (Exception e) {
                throw new RuntimeException(e);
              }
            };

    Either<String, Date> res =
        parseDate(
            Either.left(args[0]),
            s -> Date.from(LocalDateTime.parse(s).toInstant(ZoneOffset.UTC)),
            s -> Date.from(OffsetDateTime.parse(s).toInstant()),
            s -> Date.from(ZonedDateTime.parse(s).toInstant()),
            simpleDate.f("yyyy-MM-dd HH:mm:ss"),
            s -> "now".equals(s) ? new Date() : null);

    System.out.println("------");

    System.out.println(res);
  }
コード例 #8
0
 @Test(dataProvider = "parseWithOffsetWithoutZone")
 public void testWithOffsetWithoutZone(String odtString, OffsetDateTime expectedOTD) {
   dtFormatter = DateTimeFormatter.ISO_OFFSET_DATE_TIME;
   odt1 = OffsetDateTime.parse(odtString, dtFormatter);
   assertEquals(expectedOTD, odt1);
 }