本文整理了Java中java.time.format.DateTimeFormatter.format()
方法的一些代码示例,展示了DateTimeFormatter.format()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。DateTimeFormatter.format()
方法的具体详情如下:
包路径:java.time.format.DateTimeFormatter
类名称:DateTimeFormatter
方法名:format
[英]Formats a date-time object using this formatter.
This formats the date-time to a String using the rules of the formatter.
[中]使用此格式化程序格式化日期时间对象。
这将使用格式化程序的规则将日期时间格式化为字符串。
代码示例来源:origin: spring-projects/spring-framework
/**
* Set the given date under the given header name after formatting it as a string
* using the RFC-1123 date-time formatter. The equivalent of
* {@link #set(String, String)} but for date headers.
* @since 5.0
*/
public void setZonedDateTime(String headerName, ZonedDateTime date) {
set(headerName, DATE_FORMATTERS[0].format(date));
}
代码示例来源:origin: spring-projects/spring-framework
static String formatDate(long date) {
Instant instant = Instant.ofEpochMilli(date);
ZonedDateTime time = ZonedDateTime.ofInstant(instant, GMT);
return DATE_FORMATTERS[0].format(time);
}
代码示例来源:origin: eclipse-vertx/vert.x
@Override
public void serialize(Instant value, JsonGenerator jgen, SerializerProvider provider) throws IOException {
jgen.writeString(ISO_INSTANT.format(value));
}
}
代码示例来源:origin: eclipse-vertx/vert.x
/**
* Add a Instant value to the JSON array.
* <p>
* JSON has no notion of Temporal data so the Instant will be ISOString encoded, and the String added.
*
* @param value the value
* @return a reference to this, so the API can be used fluently
*/
public JsonArray add(Instant value) {
Objects.requireNonNull(value);
list.add(ISO_INSTANT.format(value));
return this;
}
代码示例来源:origin: spring-projects/spring-framework
@Override
public String print(TemporalAccessor partial, Locale locale) {
return DateTimeContextHolder.getFormatter(this.formatter, locale).format(partial);
}
代码示例来源:origin: prestodb/presto
@Override
public void serialize(ZonedDateTime value, JsonGenerator gen, SerializerProvider serializers) throws IOException,
JsonProcessingException {
/*
* Serialization of timezone data is unwanted (not ISO). Offset is kept, timezone info is thrown away here.
*
* Keeping timezone info is a new feature which needs to be implemented.
*/
gen.writeFieldName(DateTimeFormatter.ISO_OFFSET_DATE_TIME.format(value));
}
代码示例来源:origin: alibaba/fastjson
private void write(SerializeWriter out, TemporalAccessor object, String format) {
DateTimeFormatter formatter;
if ("unixtime".equals(format) && object instanceof ChronoZonedDateTime) {
long seconds = ((ChronoZonedDateTime) object).toEpochSecond();
out.writeInt((int) seconds);
return;
}
if (format == formatter_iso8601_pattern) {
formatter = formatter_iso8601;
} else {
formatter = DateTimeFormatter.ofPattern(format);
}
String text = formatter.format((TemporalAccessor) object);
out.writeString(text);
}
}
代码示例来源:origin: prestodb/presto
@Override
public void serialize(ZonedDateTime value, JsonGenerator g, SerializerProvider provider)
throws IOException
{
if (!useTimestamp(provider)) {
if (shouldWriteWithZoneId(provider)) {
// write with zone
g.writeString(DateTimeFormatter.ISO_ZONED_DATE_TIME.format(value));
return;
}
}
super.serialize(value, g, provider);
}
代码示例来源:origin: spring-projects/spring-framework
@Test
public void shouldHandleLastModifiedWithHttp304() throws Exception {
long currentTime = new Date().getTime();
long oneMinuteAgo = currentTime - (1000 * 60);
ZonedDateTime dateTime = ofEpochMilli(currentTime).atZone(GMT);
servletRequest.addHeader(HttpHeaders.IF_MODIFIED_SINCE, RFC_1123_DATE_TIME.format(dateTime));
ResponseEntity<String> returnValue = ResponseEntity.ok().lastModified(oneMinuteAgo).body("body");
initStringMessageConversion(TEXT_PLAIN);
processor.handleReturnValue(returnValue, returnTypeResponseEntity, mavContainer, webRequest);
assertConditionalResponse(HttpStatus.NOT_MODIFIED, null, null, oneMinuteAgo);
}
代码示例来源:origin: spring-projects/spring-framework
@Test // SPR-13626
public void shouldHandleIfNoneMatchIfUnmodifiedSince() throws Exception {
String etagValue = "\"some-etag\"";
servletRequest.addHeader(HttpHeaders.IF_NONE_MATCH, etagValue);
ZonedDateTime dateTime = ofEpochMilli(new Date().getTime()).atZone(GMT);
servletRequest.addHeader(HttpHeaders.IF_UNMODIFIED_SINCE, RFC_1123_DATE_TIME.format(dateTime));
ResponseEntity<String> returnValue = ResponseEntity.ok().eTag(etagValue).body("body");
initStringMessageConversion(TEXT_PLAIN);
processor.handleReturnValue(returnValue, returnTypeResponseEntity, mavContainer, webRequest);
assertConditionalResponse(HttpStatus.NOT_MODIFIED, null, etagValue, -1);
}
代码示例来源:origin: spring-projects/spring-framework
@Test
public void createDateTimeFormatterWithTimeZone() {
factory.setPattern("yyyyMMddHHmmss Z");
factory.setTimeZone(TEST_TIMEZONE);
ZoneId dateTimeZone = TEST_TIMEZONE.toZoneId();
ZonedDateTime dateTime = ZonedDateTime.of(2009, 10, 21, 12, 10, 00, 00, dateTimeZone);
String offset = (TEST_TIMEZONE.equals(NEW_YORK) ? "-0400" : "+0200");
assertThat(factory.createDateTimeFormatter().format(dateTime), is("20091021121000 " + offset));
}
代码示例来源:origin: spring-projects/spring-framework
@Test
public void shouldNotFailPreconditionForPutRequests() throws Exception {
servletRequest.setMethod("PUT");
ZonedDateTime dateTime = ofEpochMilli(new Date().getTime()).atZone(GMT);
servletRequest.addHeader(HttpHeaders.IF_UNMODIFIED_SINCE, RFC_1123_DATE_TIME.format(dateTime));
long justModified = dateTime.plus(1, ChronoUnit.SECONDS).toEpochSecond() * 1000;
ResponseEntity<String> returnValue = ResponseEntity.ok()
.lastModified(justModified).body("body");
initStringMessageConversion(TEXT_PLAIN);
processor.handleReturnValue(returnValue, returnTypeResponseEntity, mavContainer, webRequest);
assertConditionalResponse(HttpStatus.OK, null, null, justModified);
}
代码示例来源:origin: spring-projects/spring-framework
@Test
public void createDateTimeFormatterInOrderOfPropertyPriority() {
factory.setStylePattern("SS");
String value = applyLocale(factory.createDateTimeFormatter()).format(dateTime);
assertTrue(value.startsWith("10/21/09"));
assertTrue(value.endsWith("12:10 PM"));
factory.setIso(ISO.DATE);
assertThat(applyLocale(factory.createDateTimeFormatter()).format(dateTime), is("2009-10-21"));
factory.setPattern("yyyyMMddHHmmss");
assertThat(factory.createDateTimeFormatter().format(dateTime), is("20091021121000"));
}
代码示例来源:origin: spring-projects/spring-framework
@Test
public void createDateTimeFormatterWithPattern() {
factory = new DateTimeFormatterFactory("yyyyMMddHHmmss");
DateTimeFormatter formatter = factory.createDateTimeFormatter();
assertThat(formatter.format(dateTime), is("20091021121000"));
}
代码示例来源:origin: spring-projects/spring-framework
@Test
public void shouldHandleETagAndLastModifiedWithHttp304() throws Exception {
long currentTime = new Date().getTime();
long oneMinuteAgo = currentTime - (1000 * 60);
String etagValue = "\"deadb33f8badf00d\"";
ZonedDateTime dateTime = ofEpochMilli(currentTime).atZone(GMT);
servletRequest.addHeader(HttpHeaders.IF_MODIFIED_SINCE, RFC_1123_DATE_TIME.format(dateTime));
servletRequest.addHeader(HttpHeaders.IF_NONE_MATCH, etagValue);
ResponseEntity<String> returnValue = ResponseEntity.ok()
.eTag(etagValue).lastModified(oneMinuteAgo).body("body");
initStringMessageConversion(TEXT_PLAIN);
processor.handleReturnValue(returnValue, returnTypeResponseEntity, mavContainer, webRequest);
assertConditionalResponse(HttpStatus.NOT_MODIFIED, null, etagValue, oneMinuteAgo);
}
代码示例来源:origin: spring-projects/spring-framework
@Test
public void shouldHandleChangedETagAndLastModified() throws Exception {
long currentTime = new Date().getTime();
long oneMinuteAgo = currentTime - (1000 * 60);
String etagValue = "\"deadb33f8badf00d\"";
String changedEtagValue = "\"changed-etag-value\"";
ZonedDateTime dateTime = ofEpochMilli(currentTime).atZone(GMT);
servletRequest.addHeader(HttpHeaders.IF_MODIFIED_SINCE, RFC_1123_DATE_TIME.format(dateTime));
servletRequest.addHeader(HttpHeaders.IF_NONE_MATCH, etagValue);
ResponseEntity<String> returnValue = ResponseEntity.ok()
.eTag(changedEtagValue).lastModified(oneMinuteAgo).body("body");
initStringMessageConversion(TEXT_PLAIN);
processor.handleReturnValue(returnValue, returnTypeResponseEntity, mavContainer, webRequest);
assertConditionalResponse(HttpStatus.OK, null, changedEtagValue, oneMinuteAgo);
}
代码示例来源:origin: spring-projects/spring-framework
@Test
public void firstZonedDateTime() {
ZonedDateTime date = ZonedDateTime.of(2017, 6, 22, 22, 22, 0, 0, ZoneId.of("GMT"));
headers.setZonedDateTime(HttpHeaders.DATE, date);
assertThat(headers.getFirst(HttpHeaders.DATE), is("Thu, 22 Jun 2017 22:22:00 GMT"));
assertTrue(headers.getFirstZonedDateTime(HttpHeaders.DATE).isEqual(date));
headers.clear();
ZonedDateTime otherDate = ZonedDateTime.of(2010, 12, 18, 10, 20, 0, 0, ZoneId.of("GMT"));
headers.add(HttpHeaders.DATE, RFC_1123_DATE_TIME.format(date));
headers.add(HttpHeaders.DATE, RFC_1123_DATE_TIME.format(otherDate));
assertTrue(headers.getFirstZonedDateTime(HttpHeaders.DATE).isEqual(date));
// obsolete RFC 850 format
headers.clear();
headers.set(HttpHeaders.DATE, "Thursday, 22-Jun-17 22:22:00 GMT");
assertTrue(headers.getFirstZonedDateTime(HttpHeaders.DATE).isEqual(date));
// ANSI C's asctime() format
headers.clear();
headers.set(HttpHeaders.DATE, "Thu Jun 22 22:22:00 2017");
assertTrue(headers.getFirstZonedDateTime(HttpHeaders.DATE).isEqual(date));
}
代码示例来源:origin: spring-projects/spring-framework
@Test
public void notModifiedLastModified() {
ZonedDateTime now = ZonedDateTime.now();
ZonedDateTime oneMinuteBeforeNow = now.minus(1, ChronoUnit.MINUTES);
RenderingResponse responseMono = RenderingResponse.create("bar")
.header(HttpHeaders.LAST_MODIFIED, DateTimeFormatter.RFC_1123_DATE_TIME.format(oneMinuteBeforeNow))
.build()
.block();
MockServerHttpRequest request = MockServerHttpRequest.get("http://example.com")
.header(HttpHeaders.IF_MODIFIED_SINCE,
DateTimeFormatter.RFC_1123_DATE_TIME.format(now))
.build();
MockServerWebExchange exchange = MockServerWebExchange.from(request);
responseMono.writeTo(exchange, DefaultServerResponseBuilderTests.EMPTY_CONTEXT);
MockServerHttpResponse response = exchange.getResponse();
assertEquals(HttpStatus.NOT_MODIFIED, response.getStatusCode());
StepVerifier.create(response.getBody())
.expectError(IllegalStateException.class)
.verify();
}
代码示例来源:origin: spring-projects/spring-framework
@Test
public void notModifiedLastModified() {
ZonedDateTime now = ZonedDateTime.now();
ZonedDateTime oneMinuteBeforeNow = now.minus(1, ChronoUnit.MINUTES);
EntityResponse<String> responseMono = EntityResponse.fromObject("bar")
.lastModified(oneMinuteBeforeNow)
.build()
.block();
MockServerHttpRequest request = MockServerHttpRequest.get("http://example.com")
.header(HttpHeaders.IF_MODIFIED_SINCE,
DateTimeFormatter.RFC_1123_DATE_TIME.format(now))
.build();
MockServerWebExchange exchange = MockServerWebExchange.from(request);
responseMono.writeTo(exchange, DefaultServerResponseBuilderTests.EMPTY_CONTEXT);
MockServerHttpResponse response = exchange.getResponse();
assertEquals(HttpStatus.NOT_MODIFIED, response.getStatusCode());
StepVerifier.create(response.getBody())
.expectError(IllegalStateException.class)
.verify();
}
代码示例来源:origin: spring-projects/spring-framework
@Test
public void notModifiedLastModified() {
ZonedDateTime now = ZonedDateTime.now();
ZonedDateTime oneMinuteBeforeNow = now.minus(1, ChronoUnit.MINUTES);
ServerResponse responseMono = ServerResponse.ok()
.lastModified(oneMinuteBeforeNow)
.syncBody("bar")
.block();
MockServerHttpRequest request = MockServerHttpRequest.get("http://example.com")
.header(HttpHeaders.IF_MODIFIED_SINCE,
DateTimeFormatter.RFC_1123_DATE_TIME.format(now))
.build();
MockServerWebExchange exchange = MockServerWebExchange.from(request);
responseMono.writeTo(exchange, EMPTY_CONTEXT);
MockServerHttpResponse response = exchange.getResponse();
assertEquals(HttpStatus.NOT_MODIFIED, response.getStatusCode());
StepVerifier.create(response.getBody())
.expectError(IllegalStateException.class)
.verify();
}
内容来源于网络,如有侵权,请联系作者删除!