Java Code Examples for java.time.OffsetDateTime#format()

The following examples show how to use java.time.OffsetDateTime#format() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DateTimeUtilsTest.java    From kogito-runtimes with Apache License 2.0 6 votes vote down vote up
@Test
public void testParseRepeatableStartDateTimeAndPeriod() {
    OffsetDateTime oneMinuteFromNow = OffsetDateTime.now().plusMinutes(1);
    String oneMinuteFromNowFormatted = oneMinuteFromNow.format(DateTimeFormatter.ISO_DATE_TIME);
    String isoString = "R5/" + oneMinuteFromNowFormatted  + "/PT1M";

    long[] parsedRepeatable = DateTimeUtils.parseRepeatableDateTime(isoString);

    assertEquals(5L, parsedRepeatable[0]);
    assertTrue(parsedRepeatable[1] <= MINUTE_IN_MILLISECONDS,
                          "Parsed delay is bigger than " + MINUTE_IN_MILLISECONDS);
    assertTrue(parsedRepeatable[1] > FIFTY_NINE_SECONDS_IN_MILLISECONDS,
                          "Parsed delay is too low! Expected value is between " + MINUTE_IN_MILLISECONDS + " and " + FIFTY_NINE_SECONDS_IN_MILLISECONDS + " but is " + parsedRepeatable[1]);
    assertEquals(MINUTE_IN_MILLISECONDS, parsedRepeatable[2],
                            "Parsed period should be one minute in milliseconds but is " + parsedRepeatable[2]);
}
 
Example 2
Source File: DateTimeUtilsTest.java    From kogito-runtimes with Apache License 2.0 6 votes vote down vote up
@Test
public void testParseRepeatablePeriodAndEndDateTime() {
    OffsetDateTime twoMinutesFromNow = OffsetDateTime.now().plusMinutes(2);
    String twoMinutesFromNowFormatted = twoMinutesFromNow.format(DateTimeFormatter.ISO_DATE_TIME);
    String isoString = "R5/PT1M/" + twoMinutesFromNowFormatted;

    long[] parsedRepeatable = DateTimeUtils.parseRepeatableDateTime(isoString);

    assertEquals(5L, parsedRepeatable[0]);
    assertTrue(parsedRepeatable[1] <= MINUTE_IN_MILLISECONDS,
                          "Parsed delay is bigger than " + MINUTE_IN_MILLISECONDS);
    assertTrue(parsedRepeatable[1] > FIFTY_NINE_SECONDS_IN_MILLISECONDS,
                          "Parsed delay is too low! Expected value is between " + MINUTE_IN_MILLISECONDS + " and " + FIFTY_NINE_SECONDS_IN_MILLISECONDS + " but is " + parsedRepeatable[1]);
    assertEquals(MINUTE_IN_MILLISECONDS, parsedRepeatable[2],
                            "Parsed period should be one minute in milliseconds but is " + parsedRepeatable[2]);
}
 
Example 3
Source File: DateTimeUtilsTest.java    From kogito-runtimes with Apache License 2.0 6 votes vote down vote up
@Test
public void testParseRepeatableStartEndDateTime() {
    OffsetDateTime oneMinuteFromNow = OffsetDateTime.now().plusMinutes(1);
    OffsetDateTime twoMinutesFromNow = oneMinuteFromNow.plusMinutes(1);
    String oneMinuteFromNowFormatted = oneMinuteFromNow.format(DateTimeFormatter.ISO_DATE_TIME);
    String twoMinutesFromNowFormatted = twoMinutesFromNow.format(DateTimeFormatter.ISO_DATE_TIME);
    String isoString = "R5/" + oneMinuteFromNowFormatted  + "/" + twoMinutesFromNowFormatted;

    long[] parsedRepeatable = DateTimeUtils.parseRepeatableDateTime(isoString);

    assertEquals(5L, parsedRepeatable[0]);
    assertTrue(parsedRepeatable[1] <= MINUTE_IN_MILLISECONDS,
                          "Parsed delay is bigger than " + MINUTE_IN_MILLISECONDS);
    assertTrue(parsedRepeatable[1] > FIFTY_NINE_SECONDS_IN_MILLISECONDS,
                          "Parsed delay is too low! Expected value is between " + MINUTE_IN_MILLISECONDS + " and " + FIFTY_NINE_SECONDS_IN_MILLISECONDS + " but is " + parsedRepeatable[1]);
    assertEquals(MINUTE_IN_MILLISECONDS, parsedRepeatable[2],
                            "Parsed period should be one minute in milliseconds but is " + parsedRepeatable[2]);
}
 
Example 4
Source File: WebTemplateFunctions.java    From jweb-cms with GNU Affero General Public License v3.0 6 votes vote down vote up
public String fromNow(OffsetDateTime timestamp, String language) {
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MMM d", Locale.forLanguageTag(language));
    Duration duration = Duration.between(timestamp, OffsetDateTime.now());
    long seconds = duration.getSeconds();
    if (seconds <= 0) {
        return timestamp.format(formatter);
    }
    if (seconds - 60 < 0) {
        return String.format("1 %s", i18n("timeInterval.minuteBefore", language));
    }
    long minutes = duration.toMinutes();
    if (minutes - 60 < 0) {
        return String.format("%d %s", minutes, i18n("timeInterval.minutesBefore", language));
    }
    long hours = duration.toHours();
    if (hours - 2 < 0) {
        return String.format("1 %s", i18n("timeInterval.hourBefore", language));
    }
    if (hours - 24 < 0) {
        return String.format("%d %s", hours, i18n("timeInterval.hoursBefore", language));
    }
    return timestamp.format(formatter);
}
 
Example 5
Source File: PNGMetadata.java    From Bytecoder with Apache License 2.0 6 votes vote down vote up
void encodeImageCreationTimeToTextChunk() {
    // Check if Standard/Document/ImageCreationTime exists.
    if (creation_time_present) {
        // Check if a text chunk with creation time exists.
        if (tEXt_creation_time_present == false) {
            // No text chunk exists with image creation time. Add an entry.
            this.tEXt_keyword.add(tEXt_creationTimeKey);
            this.tEXt_text.add("Creation Time Place Holder");

            // Update the iterator
            int index = tEXt_text.size() - 1;
            setCreationTimeChunk(tEXt_text.listIterator(index));
        }

        // Encode image creation time with RFC1123 formatter
        OffsetDateTime offDateTime = OffsetDateTime.of(creation_time_year,
                creation_time_month, creation_time_day,
                creation_time_hour, creation_time_minute,
                creation_time_second, 0, creation_time_offset);
        DateTimeFormatter formatter = DateTimeFormatter.RFC_1123_DATE_TIME;
        String encodedTime = offDateTime.format(formatter);
        setEncodedTime(encodedTime);
    }
}
 
Example 6
Source File: PythonClientExperimentalCodegen.java    From openapi-generator with Apache License 2.0 5 votes vote down vote up
public String dateToString(Schema p, OffsetDateTime date, DateTimeFormatter dateFormatter, DateTimeFormatter dateTimeFormatter) {
    // converts a date into a date or date-time python string
    if (!(ModelUtils.isDateSchema(p) || ModelUtils.isDateTimeSchema(p))) {
        throw new RuntimeException("passed schema must be of type Date or DateTime");
    }
    if (ModelUtils.isDateSchema(p)) {
        return "dateutil_parser('" + date.format(dateFormatter) + "').date()";
    }
    return "dateutil_parser('" + date.format(dateTimeFormatter) + "')";
}
 
Example 7
Source File: Utils.java    From vertx-rabbitmq-client with Apache License 2.0 5 votes vote down vote up
public static void put(String field, Date value, JsonObject json) {
  if (value == null) return;

  OffsetDateTime date = OffsetDateTime.ofInstant(value.toInstant(), ZoneId.of("UTC"));
  String format = date.format(dateTimeFormatter);
  json.put(field, format);
}
 
Example 8
Source File: OffsetDateTimeTypeAdapter.java    From javers with Apache License 2.0 4 votes vote down vote up
@Override
public String serialize(OffsetDateTime sourceValue) {
    return sourceValue.format(ISO_FORMAT);
}
 
Example 9
Source File: DefaultSearchDocumentProducer.java    From nexus-public with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * Formats the given {@link OffsetDateTime} as an ISO timestamp.
 */
private static String format(final OffsetDateTime value) {
  return value.format(DATE_TIME_FORMATTER);
}
 
Example 10
Source File: TemplateFunctions.java    From jweb-cms with GNU Affero General Public License v3.0 4 votes vote down vote up
public String format(OffsetDateTime value, String pattern) {
    return value.format(DateTimeFormatter.ofPattern(pattern));
}
 
Example 11
Source File: EventSerializer.java    From ic with MIT License 4 votes vote down vote up
String formatDateTime(Date date) {
    Preconditions.checkNotNull(date);
    //调用truncatedTo是为了去掉毫秒,最终生成的格式如2018-08-28T14:28:21+08:00
    OffsetDateTime offsetDateTime = OffsetDateTime.ofInstant(date.toInstant(), ZoneId.of("UTC+8"));
    return offsetDateTime.format(formatter);
}
 
Example 12
Source File: TimeUtil.java    From JDA with Apache License 2.0 2 votes vote down vote up
/**
 * Returns a prettier String-representation of a OffsetDateTime object
 *
 * @param  time
 *         The OffsetDateTime object to format
 *
 * @return The String of the formatted OffsetDateTime
 */
@Nonnull
public static String getDateTimeString(@Nonnull OffsetDateTime time)
{
    return time.format(dtFormatter);
}
 
Example 13
Source File: DateTimeExtensions.java    From groovy with Apache License 2.0 2 votes vote down vote up
/**
 * Formats this date/time with the provided {@link java.time.format.DateTimeFormatter} pattern.
 *
 * @param self    an OffsetDateTime
 * @param pattern the formatting pattern
 * @return a formatted String
 * @see java.time.format.DateTimeFormatter
 * @since 2.5.0
 */
public static String format(final OffsetDateTime self, String pattern) {
    return self.format(DateTimeFormatter.ofPattern(pattern));
}
 
Example 14
Source File: DateTimeExtensions.java    From groovy with Apache License 2.0 2 votes vote down vote up
/**
 * Formats this date/time in the provided, localized {@link java.time.format.FormatStyle}.
 *
 * @param self          an OffsetDateTime
 * @param dateTimeStyle the FormatStyle
 * @return a formatted String
 * @see java.time.format.DateTimeFormatter
 * @since 2.5.0
 */
public static String format(final OffsetDateTime self, FormatStyle dateTimeStyle) {
    return self.format(DateTimeFormatter.ofLocalizedDateTime(dateTimeStyle));
}
 
Example 15
Source File: DateTimeExtensions.java    From groovy with Apache License 2.0 2 votes vote down vote up
/**
 * Formats this date/time with the {@link java.time.format.DateTimeFormatter#ISO_OFFSET_DATE_TIME} formatter.
 *
 * @param self an OffsetDateTime
 * @return a formatted String
 * @see java.time.format.DateTimeFormatter
 * @since 2.5.0
 */
public static String getDateTimeString(final OffsetDateTime self) {
    return self.format(DateTimeFormatter.ISO_OFFSET_DATE_TIME);
}
 
Example 16
Source File: DateTimeExtensions.java    From groovy with Apache License 2.0 2 votes vote down vote up
/**
 * Formats this date/time with the {@link java.time.format.DateTimeFormatter#ISO_OFFSET_DATE} formatter.
 *
 * @param self an OffsetDateTime
 * @return a formatted String
 * @see java.time.format.DateTimeFormatter
 * @since 2.5.0
 */
public static String getDateString(final OffsetDateTime self) {
    return self.format(DateTimeFormatter.ISO_OFFSET_DATE);
}
 
Example 17
Source File: DateTimeExtensions.java    From groovy with Apache License 2.0 2 votes vote down vote up
/**
 * Formats this date/time with the {@link java.time.format.DateTimeFormatter#ISO_OFFSET_TIME} formatter.
 *
 * @param self an OffsetDateTime
 * @return a formatted String
 * @see java.time.format.DateTimeFormatter
 * @since 2.5.0
 */
public static String getTimeString(final OffsetDateTime self) {
    return self.format(DateTimeFormatter.ISO_OFFSET_TIME);
}