java If-Modified-Since 日期格式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13593796/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 13:20:24  来源:igfitidea点击:

If-Modified-Since Date Format

javadatedate-format

提问by Ken

I am writing a server and I would like to check for "If-Modified-Since: " header.
Since there are so many date methods, which methods should I consider to check, like a usual method (milliseconds is used in browsers)....

我正在编写一个服务器,我想检查“If-Modified-Since:”标题。
由于日期方法太多,我应该考虑检查哪些方法,就像通常的方法(浏览器中使用毫秒)...

Following is how I did it for milliseconds format:

以下是我对毫秒格式的处理方式:

  Date date;

 //dateS is a substring of If-Modified-Since: header
 try{
  long mills = Long.parseLong(dateS);
  } catch (NumberFormatException e)
   {e.printStackTrace();
  }
  date = new Date(mills);

I also want to check for "Wed, 19 Oct 2005 10:50:00 GMT" format. How can I change that date into milliseconds?

我还想检查“2005 年 10 月 19 日星期三 10:50:00 GMT”格式。如何将该日期更改为毫秒?

     SimpleDateFormat dateFormat = new SimpleDateFormat(
                "EEE, dd MMM yyyy HH:mm:ss z");

        // to check if-modified-since time is in the above format
        try {

            ifModifiedSince = dateFormat.parse(date);
            ?????????????????????
        } catch (ParseException e1) {
        }

Please help me with chaging the above and please tell me if there is any Date format that I should check for…

请帮我修改以上内容,并告诉我是否有任何日期格式需要检查……

回答by MannVoo

HTTP applications have historically allowed three different formats for the representation of date/time stamps:

HTTP 应用程序历来允许使用三种不同的格式来表示日期/时间戳:

Sun, 06 Nov 1994 08:49:37 GMT  ; RFC 822, updated by RFC 1123
Sunday, 06-Nov-94 08:49:37 GMT ; RFC 850, obsoleted by RFC 1036
Sun Nov  6 08:49:37 1994       ; ANSI C's asctime() format

More details in HTTP/1.1 RFC 2616.

HTTP/1.1 RFC 2616 中的更多详细信息。

回答by Reimeus

As you already have the Dateobject, you can use:

由于您已经拥有该Date对象,您可以使用:

long timeInMillis = ifModifiedSince.getTime();