java.time.OffsetDateTime.format()方法的使用及代码示例

x33g5p2x  于2022-01-25 转载在 其他  
字(11.0k)|赞(0)|评价(0)|浏览(151)

本文整理了Java中java.time.OffsetDateTime.format()方法的一些代码示例,展示了OffsetDateTime.format()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。OffsetDateTime.format()方法的具体详情如下:
包路径:java.time.OffsetDateTime
类名称:OffsetDateTime
方法名:format

OffsetDateTime.format介绍

[英]Outputs this date-time as a String using the formatter.

This date-time will be passed to the formatter DateTimeFormatter#format(TemporalAccessor).
[中]使用格式化程序将此日期时间作为字符串输出。
此日期时间将传递给格式化程序DateTimeFormatter#format(TemporalAccessor)。

代码示例

代码示例来源:origin: codecentric/spring-boot-admin

  1. @Override
  2. public Map<String, String> getMetadata() {
  3. return singletonMap("startup", this.timestamp.format(DateTimeFormatter.ISO_DATE_TIME));
  4. }
  5. }

代码示例来源:origin: stackoverflow.com

  1. DateTimeFormatter formatter = DateTimeFormatter
  2. .ofPattern("yyyy-MM-dd'T'HH:mm:ss,nnnnnnnnnZ");
  3. OffsetDateTime odt = OffsetDateTime.of(2015, 11, 2, 12, 38, 0, 123456789, ZoneOffset.UTC);
  4. System.out.println(odt.format(formatter));

代码示例来源:origin: prontera/spring-cloud-rest-tcc

  1. @Override
  2. public void serialize(OffsetDateTime dateTime, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
  3. jsonGenerator.writeString(dateTime.format(DateTimeFormatter.ISO_OFFSET_DATE_TIME));
  4. }
  5. }

代码示例来源:origin: confluentinc/ksql

  1. public String timestamp() {
  2. if (timestamp == 0) {
  3. return "n/a";
  4. }
  5. return Instant.ofEpochMilli(timestamp)
  6. .atOffset(ZoneOffset.UTC)
  7. .format(DateTimeFormatter.ISO_OFFSET_DATE_TIME);
  8. }

代码示例来源:origin: apache/ignite

  1. /** {@inheritDoc} */
  2. @Override public String getStartTimeLocal() {
  3. return statMgr.startTime().format(DateTimeFormatter.ISO_DATE_TIME);
  4. }

代码示例来源:origin: eclipse-vertx/vert.x

  1. @Override
  2. public String format(final LogRecord record) {
  3. OffsetDateTime date = fromMillis(record.getMillis());
  4. StringBuilder sb = new StringBuilder();
  5. // Minimize memory allocations here.
  6. sb.append("[").append(Thread.currentThread().getName()).append("] ");
  7. sb.append(date.format(DateTimeFormatter.ISO_OFFSET_DATE_TIME)).append(" ");
  8. sb.append(record.getLevel()).append(" [");
  9. sb.append(record.getLoggerName()).append("]").append(" ");
  10. sb.append(record.getMessage());
  11. sb.append(Utils.LINE_SEPARATOR);
  12. if (record.getThrown() != null) {
  13. try {
  14. StringWriter sw = new StringWriter();
  15. PrintWriter pw = new PrintWriter(sw);
  16. record.getThrown().printStackTrace(pw);
  17. pw.close();
  18. sb.append(sw.toString());
  19. } catch (Exception ex) {
  20. ex.printStackTrace();
  21. }
  22. }
  23. return sb.toString();
  24. }

代码示例来源:origin: debezium/debezium

  1. /**
  2. * Get the ISO 8601 formatted representation of the given {@link OffsetDateTime}.
  3. *
  4. * @param timestamp the timestamp value
  5. * @param adjuster the optional component that adjusts the local date value before obtaining the epoch day; may be null if no
  6. * adjustment is necessary
  7. * @return the ISO 8601 formatted string
  8. */
  9. public static String toIsoString(OffsetDateTime timestamp, TemporalAdjuster adjuster) {
  10. if (adjuster != null) {
  11. timestamp = timestamp.with(adjuster);
  12. }
  13. return timestamp.format(FORMATTER);
  14. }

代码示例来源:origin: kiegroup/optaplanner

  1. public String getStartingTimestampAsMediumString() {
  2. return startingTimestamp == null ? null : startingTimestamp.format(DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM));
  3. }

代码示例来源:origin: kiegroup/jbpm

  1. public static String[] parseISORepeatable(String isoString) {
  2. String[] result = new String[3];
  3. String[] elements = isoString.split("/");
  4. if (elements.length==3) {
  5. result[0] = elements[0].substring(1);
  6. result[1] = elements[1];
  7. result[2] = elements[2];
  8. } else {
  9. result[0] = elements[0].substring(1);
  10. result[1] = OffsetDateTime.now().format(DateTimeFormatter.ISO_DATE_TIME);
  11. result[2] = elements[1];
  12. }
  13. return result;
  14. }

代码示例来源:origin: kiegroup/optaplanner

  1. public void initBenchmarkReportDirectory(File benchmarkDirectory) {
  2. String timestampString = startingTimestamp.format(DateTimeFormatter.ofPattern("yyyy-MM-dd_HHmmss"));
  3. if (StringUtils.isEmpty(name)) {
  4. name = timestampString;
  5. }
  6. if (!benchmarkDirectory.mkdirs()) {
  7. if (!benchmarkDirectory.isDirectory()) {
  8. throw new IllegalArgumentException("The benchmarkDirectory (" + benchmarkDirectory
  9. + ") already exists, but is not a directory.");
  10. }
  11. if (!benchmarkDirectory.canWrite()) {
  12. throw new IllegalArgumentException("The benchmarkDirectory (" + benchmarkDirectory
  13. + ") already exists, but is not writable.");
  14. }
  15. }
  16. int duplicationIndex = 0;
  17. do {
  18. String directoryName = timestampString + (duplicationIndex == 0 ? "" : "_" + duplicationIndex);
  19. duplicationIndex++;
  20. benchmarkReportDirectory = new File(benchmarkDirectory,
  21. BooleanUtils.isFalse(aggregation) ? directoryName : directoryName + "_aggregation");
  22. } while (!benchmarkReportDirectory.mkdir());
  23. for (ProblemBenchmarkResult problemBenchmarkResult : unifiedProblemBenchmarkResultList) {
  24. problemBenchmarkResult.makeDirs();
  25. }
  26. }

代码示例来源:origin: apache/drill

  1. /**
  2. * Applies local time zone offset to timestamp value read using specified {@code reader}.
  3. *
  4. * @param writer writer to store string representation of timestamp value
  5. * @param fieldName name of the field
  6. * @param reader document reader
  7. */
  8. @Override
  9. protected void writeTimeStamp(MapOrListWriterImpl writer, String fieldName, DocumentReader reader) {
  10. String formattedTimestamp = Instant.ofEpochMilli(reader.getTimestampLong())
  11. .atOffset(OffsetDateTime.now().getOffset()).format(DateUtility.UTC_FORMATTER);
  12. writeString(writer, fieldName, formattedTimestamp);
  13. }
  14. };

代码示例来源:origin: spring-projects/spring-session

  1. : Instant.EPOCH.atOffset(ZoneOffset.UTC);
  2. sb.append("; Expires=")
  3. .append(expires.format(DateTimeFormatter.RFC_1123_DATE_TIME));

代码示例来源:origin: kiegroup/jbpm

  1. @Test
  2. public void testParseDateTime() {
  3. OffsetDateTime hourAfterEpoch = OffsetDateTime.of(1970, 1, 1, 1, 0, 0, 0, ZoneOffset.UTC);
  4. long parsedMilliseconds = DateTimeUtils.parseDateTime(hourAfterEpoch.format(DateTimeFormatter.ISO_DATE_TIME));
  5. assertEquals(HOUR_IN_MILLISECONDS, parsedMilliseconds);
  6. }

代码示例来源:origin: DV8FromTheWorld/JDA

  1. /**
  2. * Returns a prettier String-representation of a OffsetDateTime object
  3. *
  4. * @param time
  5. * The OffsetDateTime object to format
  6. *
  7. * @return The String of the formatted OffsetDateTime
  8. */
  9. public static String getDateTimeString(OffsetDateTime time)
  10. {
  11. return time.format(dtFormatter);
  12. }

代码示例来源:origin: apache/ignite

  1. /**
  2. * Simple test JMX bean for caches IO stats.
  3. *
  4. * @throws Exception In case of failure.
  5. */
  6. @Test
  7. public void testCacheBasic() throws Exception {
  8. IoStatisticsMetricsMXBean bean = ioStatMXBean();
  9. IoStatisticsManager ioStatMgr = ignite.context().ioStats();
  10. Assert.assertEquals(ioStatMgr.startTime().toEpochSecond(), bean.getStartTime());
  11. Assert.assertEquals(ioStatMgr.startTime().format(DateTimeFormatter.ISO_DATE_TIME), bean.getStartTimeLocal());
  12. bean.reset();
  13. Assert.assertEquals(ioStatMgr.startTime().toEpochSecond(), bean.getStartTime());
  14. Assert.assertEquals(ioStatMgr.startTime().format(DateTimeFormatter.ISO_DATE_TIME), bean.getStartTimeLocal());
  15. int cnt = 100;
  16. warmUpMemmory(bean, cnt);
  17. populateCache(cnt);
  18. Long cacheLogicalReadsCnt = bean.getCacheGroupLogicalReads(DEFAULT_CACHE_NAME);
  19. Assert.assertNotNull(cacheLogicalReadsCnt);
  20. Assert.assertEquals(cnt, cacheLogicalReadsCnt.longValue());
  21. Long cachePhysicalReadsCnt = bean.getCacheGroupPhysicalReads(DEFAULT_CACHE_NAME);
  22. Assert.assertNotNull(cachePhysicalReadsCnt);
  23. Assert.assertEquals(0, cachePhysicalReadsCnt.longValue());
  24. String formatted = bean.getCacheGroupStatistics(DEFAULT_CACHE_NAME);
  25. Assert.assertEquals("CACHE_GROUP default [LOGICAL_READS=100, PHYSICAL_READS=0]", formatted);
  26. String unexistedStats = bean.getCacheGroupStatistics("unknownCache");
  27. Assert.assertEquals("CACHE_GROUP unknownCache []", unexistedStats);
  28. }

代码示例来源:origin: kiegroup/jbpm

  1. @Test
  2. public void testParseRepeatableStartEndDateTime() {
  3. OffsetDateTime oneMinuteFromNow = OffsetDateTime.now().plusMinutes(1);
  4. OffsetDateTime twoMinutesFromNow = oneMinuteFromNow.plusMinutes(1);
  5. String oneMinuteFromNowFormatted = oneMinuteFromNow.format(DateTimeFormatter.ISO_DATE_TIME);
  6. String twoMinutesFromNowFormatted = twoMinutesFromNow.format(DateTimeFormatter.ISO_DATE_TIME);
  7. String isoString = "R5/" + oneMinuteFromNowFormatted + "/" + twoMinutesFromNowFormatted;
  8. long[] parsedRepeatable = DateTimeUtils.parseRepeatableDateTime(isoString);
  9. assertEquals(5L, parsedRepeatable[0]);
  10. assertTrue("Parsed delay is bigger than " + MINUTE_IN_MILLISECONDS, parsedRepeatable[1] <= MINUTE_IN_MILLISECONDS);
  11. assertTrue("Parsed delay is too low! Expected value is between " + MINUTE_IN_MILLISECONDS + " and " + FIFTY_NINE_SECONDS_IN_MILLISECONDS + " but is " + parsedRepeatable[1], parsedRepeatable[1] > FIFTY_NINE_SECONDS_IN_MILLISECONDS);
  12. assertEquals("Parsed period should be one minute in milliseconds but is " + parsedRepeatable[2], MINUTE_IN_MILLISECONDS, parsedRepeatable[2]);
  13. }

代码示例来源:origin: apache/ignite

  1. Assert.assertEquals(ioStatMgr.startTime().format(DateTimeFormatter.ISO_DATE_TIME), bean.getStartTimeLocal());
  2. Assert.assertEquals(ioStatMgr.startTime().format(DateTimeFormatter.ISO_DATE_TIME), bean.getStartTimeLocal());

代码示例来源:origin: kiegroup/jbpm

  1. @Test
  2. public void testParseDateAsDuration() {
  3. OffsetDateTime oneMinuteFromNow = OffsetDateTime.now().plusMinutes(1);
  4. long parsedMilliseconds = DateTimeUtils.parseDateAsDuration(oneMinuteFromNow.format(DateTimeFormatter.ISO_DATE_TIME));
  5. assertTrue("Parsed date as duration is bigger than " + MINUTE_IN_MILLISECONDS, parsedMilliseconds <= MINUTE_IN_MILLISECONDS);
  6. assertTrue("Parsed date as duration is too low! Expected value is between " + MINUTE_IN_MILLISECONDS + " and " + FIFTY_NINE_SECONDS_IN_MILLISECONDS + " but is " + parsedMilliseconds, parsedMilliseconds > FIFTY_NINE_SECONDS_IN_MILLISECONDS);
  7. }

代码示例来源:origin: kiegroup/jbpm

  1. @Test
  2. public void testParseRepeatablePeriodAndEndDateTime() {
  3. OffsetDateTime twoMinutesFromNow = OffsetDateTime.now().plusMinutes(2);
  4. String twoMinutesFromNowFormatted = twoMinutesFromNow.format(DateTimeFormatter.ISO_DATE_TIME);
  5. String isoString = "R5/PT1M/" + twoMinutesFromNowFormatted;
  6. long[] parsedRepeatable = DateTimeUtils.parseRepeatableDateTime(isoString);
  7. assertEquals(5L, parsedRepeatable[0]);
  8. assertTrue("Parsed delay is bigger than " + MINUTE_IN_MILLISECONDS, parsedRepeatable[1] <= MINUTE_IN_MILLISECONDS);
  9. assertTrue("Parsed delay is too low! Expected value is between " + MINUTE_IN_MILLISECONDS + " and " + FIFTY_NINE_SECONDS_IN_MILLISECONDS + " but is " + parsedRepeatable[1], parsedRepeatable[1] > FIFTY_NINE_SECONDS_IN_MILLISECONDS);
  10. assertEquals("Parsed period should be one minute in milliseconds but is " + parsedRepeatable[2], MINUTE_IN_MILLISECONDS, parsedRepeatable[2]);
  11. }

代码示例来源:origin: kiegroup/jbpm

  1. @Test
  2. public void testParseRepeatableStartDateTimeAndPeriod() {
  3. OffsetDateTime oneMinuteFromNow = OffsetDateTime.now().plusMinutes(1);
  4. String oneMinuteFromNowFormatted = oneMinuteFromNow.format(DateTimeFormatter.ISO_DATE_TIME);
  5. String isoString = "R5/" + oneMinuteFromNowFormatted + "/PT1M";
  6. long[] parsedRepeatable = DateTimeUtils.parseRepeatableDateTime(isoString);
  7. assertEquals(5L, parsedRepeatable[0]);
  8. assertTrue("Parsed delay is bigger than " + MINUTE_IN_MILLISECONDS, parsedRepeatable[1] <= MINUTE_IN_MILLISECONDS);
  9. assertTrue("Parsed delay is too low! Expected value is between " + MINUTE_IN_MILLISECONDS + " and " + FIFTY_NINE_SECONDS_IN_MILLISECONDS + " but is " + parsedRepeatable[1], parsedRepeatable[1] > FIFTY_NINE_SECONDS_IN_MILLISECONDS);
  10. assertEquals("Parsed period should be one minute in milliseconds but is " + parsedRepeatable[2], MINUTE_IN_MILLISECONDS, parsedRepeatable[2]);
  11. }

相关文章