Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/380.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将GTM字符串格式的日期转换为Javascript中的日期对象_Javascript_Java - Fatal编程技术网

将GTM字符串格式的日期转换为Javascript中的日期对象

将GTM字符串格式的日期转换为Javascript中的日期对象,javascript,java,Javascript,Java,在Java中,我需要为cookie内容设置cookie到期时间,而不是为此cookie设置到期时间。下面的代码显示了这是如何完成的 public static Cookie addOPBrowserStateCookie(HttpServletResponse response) { int expiresSeconds = 120; Date date = new Date(); date = new Date(date.getTime() +1000 * expir

在Java中,我需要为cookie内容设置cookie到期时间,而不是为此cookie设置到期时间。下面的代码显示了这是如何完成的

public static Cookie addOPBrowserStateCookie(HttpServletResponse response) {

    int expiresSeconds = 120;
    Date date = new Date();
    date = new Date(date.getTime() +1000 * expiresSeconds);
    String expiryTime = date.toGMTString();
    Cookie cookie =
            new Cookie(OIDCSessionConstants.OPBS_COOKIE_ID, UUID.randomUUID().toString() + "_" + expiryTime);
    cookie.setSecure(true);
    cookie.setPath("/");

    response.addCookie(cookie);
    return cookie;
}
然后从Javascript中,我需要将这个字符串转换回日期对象,并与当前日期值进行比较。下面是我尝试的方法

    function getOPBrowserState() {
        var name = "opbs=";
        var cookie = document.cookie + ";";
        var start = cookie.indexOf(name);
        if (start != -1) {
            var end = cookie.indexOf(";", start);
            opbs = cookie.substring(start + name.length, end);
        }

        console.log(opbs);
        var c = opbs.split('_');
        var result = c[0];
        var expiry_time = Date.parse(c[1]);
        var now = Date.now();
        console.log(expiry_time.toString());

        if (expiry_time < now) {
            return null;
        }
        else {
            return result;
        }

    }
函数getOPBrowserState(){ var name=“opbs=”; var cookie=document.cookie+“;”; var start=cookie.indexOf(名称); 如果(开始!=-1){ var end=cookie.indexOf(“;”,start); opbs=cookie.substring(start+name.length,end); } 控制台日志(opbs); var c=opbs.拆分(“uUZ”); var结果=c[0]; var expiration_time=Date.parse(c[1]); var now=Date.now(); log(expiration_time.toString()); 如果(到期时间<现在){ 返回null; } 否则{ 返回结果; } } 但是,当我在字符串上使用Date.parse时,我总是得到expire\u日期的Nan值。谢谢你在这方面的帮助


谢谢

c[1]
包含什么?如果将GMT字符串传递给
new Date()
应该可以使用。它是我从Java为cookie内容设置的GMT字符串,例如“2018年5月31日07:24:01 GMT”
new Date(“2018年5月31日07:24:01 GMT”)应该可以了,谢谢!是的,这起作用了。然而,问题是,在拆分“opbs”字符串时,会在字符串的末尾随机添加一个双引号。因此,我之前得到了Nan结果。