Expires string in cookie header

前端 未结 4 1215
一整个雨季
一整个雨季 2021-02-13 11:59

Simple question I think, but I just can\'t seem to find an answer.

I am writing a cookie in a Java Servlet with the Cookie class which is sent to the browser in the resp

4条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-02-13 12:31

    Java 8 now supplies an appropriate date formatter, DateTimeFormatter.RFC_1123_DATE_TIME:

    OffsetDateTime oneHourFromNow 
            = OffsetDateTime.now(ZoneOffset.UTC)
            .plus(Duration.ofHours(1));
    
    String cookieExpires 
            = DateTimeFormatter.RFC_1123_DATE_TIME
            .format(oneHourFromNow);
    
    // E.g. "Tue, 8 Nov 2016 20:15:46 GMT"
    

    This format is valid for the the expires attribute, see RFC 6265 § 4.1.1, which defines the format to be an RFC 1123 date:

    expires-av        = "Expires=" sane-cookie-date
    sane-cookie-date  = 
    

提交回复
热议问题