11

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)....

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?

     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…

Luke Girvin
  • 13,221
  • 9
  • 64
  • 84
Ken
  • 287
  • 3
  • 5
  • 15
  • 1
    already answered I believe: [http://stackoverflow.com/questions/1930158/how-to-parse-date-from-http-last-modified-header][1] [1]: http://stackoverflow.com/questions/1930158/how-to-parse-date-from-http-last-modified-header – Farid Nov 27 '12 at 21:55
  • browsers use millisecond as Last-Modified-Date, because it is what I have been getting in request header in all browsers – Ken Nov 27 '12 at 21:58
  • in the link i was referring to use format.parse(dateString).getTime(); – Farid Nov 27 '12 at 22:02

2 Answers2

19

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

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.

MannVoo
  • 301
  • 1
  • 3
1

As you already have the Date object, you can use:

long timeInMillis = ifModifiedSince.getTime();
Reimeus
  • 158,255
  • 15
  • 216
  • 276