Expires string in cookie header

前端 未结 4 1213
一整个雨季
一整个雨季 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条回答
  •  醉梦人生
    2021-02-13 12:20

    Something like this :

    Date expdate = new Date ();
    expdate.setTime (expdate.getTime() + (3600 * 1000));
    String cookieExpire = "expires=" + expdate.toGMTString();
    ...
    

    .. and since toGMTString() is deprecated

    Date expdate= new Date();
    expdate.setTime (expdate.getTime() + (3600 * 1000));
    DateFormat df = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz", java.util.Locale.US);
    df.setTimeZone(TimeZone.getTimeZone("GMT"));
    String cookieExpire = "expires=" + df.format(expdate);
    

提交回复
热议问题