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

x33g5p2x  于2022-01-23 转载在 其他  
字(7.1k)|赞(0)|评价(0)|浏览(276)

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

LocalDateTime.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: stackoverflow.com

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
LocalDateTime dateTime = LocalDateTime.of(1986, Month.APRIL, 8, 12, 30);
String formattedDateTime = dateTime.format(formatter); // "1986-04-08 12:30"

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

@Override
public String toString() {
 return localDateTime.format(PRINT_FORMATTER);
}

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

public String format(DateTimeFormatter formatter) {
 return localDateTime.format(formatter);
}

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

LocalDateTime now = LocalDateTime.now();
String format1 = now.format(DateTimeFormatter.ISO_DATE_TIME);
String format2 = now.atZone(ZoneId.of("GMT")).format(DateTimeFormatter.RFC_1123_DATE_TIME);
String format3 = now.format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss", Locale.ENGLISH));

System.out.println(format1);
System.out.println(format2);
System.out.println(format3);

代码示例来源:origin: baomidou/mybatis-plus

/**
 * 格式化的毫秒时间
 */
public static String getMillisecond() {
  return LocalDateTime.now().format(MILLISECOND);
}

代码示例来源:origin: lets-blade/blade

public static String toString(LocalDateTime date, String pattern) {
  return date.format(DateTimeFormatter.ofPattern(pattern));
}

代码示例来源:origin: lets-blade/blade

public static String toString(LocalDateTime date, String pattern) {
  return date.format(DateTimeFormatter.ofPattern(pattern));
}

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

private String getDefaultFilename() throws UnknownHostException
{
  String hostName = InetAddress.getLocalHost().getHostName();
  String safeFilename = hostName.replaceAll( "[^a-zA-Z0-9._]+", "_" );
  return safeFilename + "-" + LocalDateTime.now().format( filenameDateTimeFormatter ) + ".zip";
}

代码示例来源:origin: yu199195/hmily

/**
 * Gets current date time.
 *
 * @return the current date time
 */
public static String getCurrentDateTime() {
  return LocalDateTime.now().format(DateTimeFormatter.ofPattern(DATE_FORMAT_DATETIME));
}

代码示例来源:origin: lets-blade/blade

/**
 * format date to string
 *
 * @param date    date instance
 * @param pattern date format pattern
 * @return return string date
 */
public static String toString(Date date, String pattern) {
  Instant instant = new java.util.Date((date.getTime())).toInstant();
  return LocalDateTime.ofInstant(instant, ZoneId.systemDefault()).format(DateTimeFormatter.ofPattern(pattern));
}

代码示例来源:origin: lets-blade/blade

/**
 * format date to string
 *
 * @param date    date instance
 * @param pattern date format pattern
 * @return return string date
 */
public static String toString(Date date, String pattern) {
  Instant instant = new java.util.Date((date.getTime())).toInstant();
  return LocalDateTime.ofInstant(instant, ZoneId.systemDefault()).format(DateTimeFormatter.ofPattern(pattern));
}

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

@Override
public String prettyPrint()
{
  return assertPrintable( () -> value.format( DateTimeFormatter.ISO_LOCAL_DATE_TIME ) );
}

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

private String generateOldPlanName(String newName, int i) {
 if (MetastoreConf.getBoolVar(conf, ConfVars.HIVE_IN_TEST)) {
  // Do not use datetime in tests to avoid result changes.
  return newName + "_old_" + i;
 } else {
  return newName + "_old_"
    + LocalDateTime.now().format(YMDHMS_FORMAT) + (i == 0 ? "" : ("_" + i));
 }
}

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

public static String getTimestampString(Timestamp ts) {
  return
    LocalDateTime.ofInstant(Instant.ofEpochMilli(ts.getTime()), ZoneOffset.UTC)
    .withNano(ts.getNanos())
    .format(PRINT_FORMATTER);
 }
}

代码示例来源:origin: ctripcorp/apollo

private ResponseEntity<Map<String, Object>> handleError(HttpServletRequest request,
                            HttpStatus status, Throwable ex, Level logLevel) {
 String message = ex.getMessage();
 printLog(message, ex, logLevel);
 Map<String, Object> errorAttributes = new HashMap<>();
 boolean errorHandled = false;
 if (ex instanceof HttpStatusCodeException) {
  try {
   //try to extract the original error info if it is thrown from apollo programs, e.g. admin service
   errorAttributes = gson.fromJson(((HttpStatusCodeException) ex).getResponseBodyAsString(), mapType);
   status = ((HttpStatusCodeException) ex).getStatusCode();
   errorHandled = true;
  } catch (Throwable th) {
   //ignore
  }
 }
 if (!errorHandled) {
  errorAttributes.put("status", status.value());
  errorAttributes.put("message", message);
  errorAttributes.put("timestamp",
    LocalDateTime.now().format(DateTimeFormatter.ISO_LOCAL_DATE_TIME));
  errorAttributes.put("exception", ex.getClass().getName());
 }
 HttpHeaders headers = new HttpHeaders();
 headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
 return new ResponseEntity<>(errorAttributes, headers, status);
}

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

@Override
protected void func(BytesColumnVector outV, TimestampColumnVector inV, int i) {
 byte[] temp = LocalDateTime.ofInstant(Instant.ofEpochMilli(inV.time[i]), ZoneOffset.UTC)
   .withNano(inV.nanos[i])
   .format(PRINT_FORMATTER).getBytes();
 assign(outV, i, temp, temp.length);
}

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

@Override
public String toString() {
 if (timestampEmpty) {
  populateTimestamp();
 }
 if (timestamp.getNanos() > 0) {
  return timestamp.toString();
 }
 return timestamp.toLocalDateTime().format(DATE_TIME_FORMAT);
}

代码示例来源:origin: testcontainers/testcontainers-java

@Test
  public void recordingFileThatShouldDescribeTheTestResultAtThePresentTime() throws Exception {
    File vncRecordingDirectory = Files.createTempDirectory("recording").toFile();
    Description description = createTestDescription(getClass().getCanonicalName(), methodName, Test.class);

    File recordingFile = factory.recordingFileForTest(vncRecordingDirectory, description, success);

    String expectedFilePrefix = format("%s-%s-%s", prefix, getClass().getSimpleName(), methodName);

    List<File> expectedPossibleFileNames = Arrays.asList(
        new File(vncRecordingDirectory, format("%s-%s.flv", expectedFilePrefix, now().format(DATETIME_FORMATTER))),
        new File(vncRecordingDirectory, format("%s-%s.flv", expectedFilePrefix, now().minusSeconds(1L).format(DATETIME_FORMATTER)))
    );

    assertThat(expectedPossibleFileNames, hasItem(recordingFile));
  }
}

代码示例来源:origin: prestodb/presto

@Override
public void serialize(LocalDateTime value, JsonGenerator g, SerializerProvider provider)
  throws IOException
{
  if (useTimestamp(provider)) {
    g.writeStartArray();
    _serializeAsArrayContents(value, g, provider);
    g.writeEndArray();
  } else {
    DateTimeFormatter dtf = _formatter;
    if (dtf == null) {
      dtf = _defaultFormatter();
    }
    g.writeString(value.format(dtf));
  }
}

代码示例来源:origin: prestodb/presto

@Override
public void serializeWithType(LocalDateTime value, JsonGenerator g, SerializerProvider provider,
    TypeSerializer typeSer) throws IOException
{
  WritableTypeId typeIdDef = typeSer.writeTypePrefix(g,
      typeSer.typeId(value, serializationShape(provider)));
  // need to write out to avoid double-writing array markers
  if (typeIdDef.valueShape == JsonToken.START_ARRAY) {
    _serializeAsArrayContents(value, g, provider);
  } else {
    DateTimeFormatter dtf = _formatter;
    if (dtf == null) {
      dtf = _defaultFormatter();
    }
    g.writeString(value.format(dtf));
  }
  typeSer.writeTypeSuffix(g, typeIdDef);
}

相关文章

LocalDateTime类方法