下面列出了java.time.OffsetDateTime#format ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@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]);
}
@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]);
}
@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]);
}
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);
}
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);
}
}
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) + "')";
}
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);
}
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);
}
public String format(OffsetDateTime value, String pattern) {
return value.format(DateTimeFormatter.ofPattern(pattern));
}
/**
* Formats the given {@link OffsetDateTime} as an ISO timestamp.
*/
private static String format(final OffsetDateTime value) {
return value.format(DATE_TIME_FORMATTER);
}
@Override
public String serialize(OffsetDateTime sourceValue) {
return sourceValue.format(ISO_FORMAT);
}
/**
* 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);
}
/**
* 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));
}
/**
* 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));
}
/**
* 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);
}
/**
* 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);
}
/**
* 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);
}