Skip to content

Commit 3671888

Browse files
committed
Allow Instant and ZonedDateTime as Last-Modified header.
Issues: SPR-17571
1 parent cf7ee07 commit 3671888

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

spring-web/src/main/java/org/springframework/http/HttpHeaders.java

+25
Original file line numberDiff line numberDiff line change
@@ -1145,6 +1145,22 @@ public void setLastModified(long lastModified) {
11451145
setDate(LAST_MODIFIED, lastModified);
11461146
}
11471147

1148+
/**
1149+
* Set the time the resource was last changed, as specified by the
1150+
* {@code Last-Modified} header.
1151+
*/
1152+
public void setLastModified(Instant lastModified) {
1153+
setInstant(LAST_MODIFIED, lastModified);
1154+
}
1155+
1156+
/**
1157+
* Set the time the resource was last changed, as specified by the
1158+
* {@code Last-Modified} header.
1159+
*/
1160+
public void setLastModified(ZonedDateTime lastModified) {
1161+
setZonedDateTime(LAST_MODIFIED, lastModified);
1162+
}
1163+
11481164
/**
11491165
* Return the time the resource was last changed, as specified by the
11501166
* {@code Last-Modified} header.
@@ -1266,6 +1282,15 @@ public void setZonedDateTime(String headerName, ZonedDateTime date) {
12661282
set(headerName, DATE_FORMATTERS[0].format(date));
12671283
}
12681284

1285+
/**
1286+
* Set the given date under the given header name after formatting it as a string
1287+
* using the RFC-1123 date-time formatter. The equivalent of
1288+
* {@link #set(String, String)} but for date headers.
1289+
*/
1290+
public void setInstant(String headerName, Instant date) {
1291+
setZonedDateTime(headerName, ZonedDateTime.ofInstant(date, GMT));
1292+
}
1293+
12691294
/**
12701295
* Set the given date under the given header name after formatting it as a string
12711296
* using the RFC-1123 date-time formatter. The equivalent of

spring-web/src/main/java/org/springframework/http/ResponseEntity.java

+32
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
package org.springframework.http;
1818

1919
import java.net.URI;
20+
import java.time.Instant;
21+
import java.time.ZonedDateTime;
2022
import java.util.Arrays;
2123
import java.util.LinkedHashSet;
2224
import java.util.Optional;
@@ -359,6 +361,24 @@ public interface HeadersBuilder<B extends HeadersBuilder<B>> {
359361
*/
360362
B lastModified(long lastModified);
361363

364+
/**
365+
* Set the time the resource was last changed, as specified by the
366+
* {@code Last-Modified} header.
367+
* @param lastModified the last modified date
368+
* @return this builder
369+
* @see HttpHeaders#setLastModified(long)
370+
*/
371+
B lastModified(ZonedDateTime lastModified);
372+
373+
/**
374+
* Set the time the resource was last changed, as specified by the
375+
* {@code Last-Modified} header.
376+
* @param lastModified the last modified date
377+
* @return this builder
378+
* @see HttpHeaders#setLastModified(long)
379+
*/
380+
B lastModified(Instant lastModified);
381+
362382
/**
363383
* Set the location of a resource, as specified by the {@code Location} header.
364384
* @param location the location
@@ -495,6 +515,18 @@ public BodyBuilder lastModified(long date) {
495515
return this;
496516
}
497517

518+
@Override
519+
public BodyBuilder lastModified(ZonedDateTime date) {
520+
this.headers.setLastModified(date);
521+
return this;
522+
}
523+
524+
@Override
525+
public BodyBuilder lastModified(Instant date) {
526+
this.headers.setLastModified(date);
527+
return this;
528+
}
529+
498530
@Override
499531
public BodyBuilder location(URI location) {
500532
this.headers.setLocation(location);

0 commit comments

Comments
 (0)