Class DateParser

java.lang.Object
org.simpleframework.util.parse.Parser
org.simpleframework.http.parse.DateParser

public class DateParser extends Parser
This is used to create a Parser for the HTTP date format. This will parse the 3 formats that are acceptable for the HTTP/1.1 date. The three formats that are acceptable for the HTTP-date are
 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
 

This can also parse the date in ms as retrived from the System's System.currentTimeMillis method. This has a parse method for a long which will do the same as the parse(String). Once the date has been parsed there are two methods that allow the date to be represented, the toLong method converts the date to a long and the toString method will convert the date into a String.

This produces the same string as the SimpleDateFormat.format using the pattern "EEE, dd MMM yyyy hh:mm:ss 'GMT'". This will however do the job faster as it does not take arbitrary inputs.

Author:
Niall Gallagher
  • Field Summary

    Fields inherited from class org.simpleframework.util.parse.Parser

    buf, count, off
  • Constructor Summary

    Constructors
    Constructor
    Description
    The default constructor will create a parser that can parse Strings that contain dates in the form of RFC 1123, RFC 850 or asctime.
    DateParser(long date)
    This constructor will conveniently parse the long argument in the constructor.
    This constructor will conveniently parse the String argument in the constructor.
  • Method Summary

    Modifier and Type
    Method
    Description
    convert(long date)
    Convenience method used to convert the specified long date in to a HTTP date format.
    long
    Convenience method used to convert the specified HTTP date in to a long representing the time.
    static String
    The parser contains this method so that the a date does not have to be parsed from System.currentTimeMillis.
    protected void
    This is used to reset the date and the buffer variables for this DateParser.
    protected void
    This is used to parse the contents of the buf.
    void
    parse(long date)
    This is used to extract the date from a long.
    long
    This returns the date in as a long, given the exact time this will use the java.util.Date to parse this date into a long.
    This prints the date in the format of a RFC 1123 date.

    Methods inherited from class org.simpleframework.util.parse.Parser

    digit, ensureCapacity, parse, skip, space, toLower

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Constructor Details

    • DateParser

      public DateParser()
      The default constructor will create a parser that can parse Strings that contain dates in the form of RFC 1123, RFC 850 or asctime. If the dates that are to be parsed are not in the form of one of these date encodings the results of this parser will be random.
    • DateParser

      public DateParser(long date)
      This constructor will conveniently parse the long argument in the constructor. This can also be done by first calling the no-arg constructor and then using the parse method.

      This will then set this object to one that uses the RFC 1123 format for a date.

      Parameters:
      date - the date to be parsed
    • DateParser

      public DateParser(String date)
      This constructor will conveniently parse the String argument in the constructor. This can also be done by first calling the no-arg constructor and then using the parse method.

      This will then set this object to one that uses the RFC 1123 format for a date.

      Parameters:
      date - the date to be parsed
  • Method Details

    • getDate

      public static String getDate()
      The parser contains this method so that the a date does not have to be parsed from System.currentTimeMillis. This returns a date using a DataParser.parse(long) method invocation.
      Returns:
      this returns a RFC 1123 date for the current time
    • parse

      public void parse(long date)
      This is used to extract the date from a long. If this method is given the value of the date as a long it will construct the RFC 1123 date as required by RFC 2616 sec 3.3.

      This saves time on parsing a String that is encoded in the HTTP-date format. The date given must be positive, if the date given is not a positive 'long' then the results of this method is random/unknown.

      Parameters:
      date - the date to be parsed
    • convert

      public long convert(String date)
      Convenience method used to convert the specified HTTP date in to a long representing the time. This is used when a single method is required to convert a HTTP date format to a usable long value for use in creating Date objects.
      Parameters:
      date - the date specified in on of the HTTP date formats
      Returns:
      the date value as a long value in milliseconds
    • convert

      public String convert(long date)
      Convenience method used to convert the specified long date in to a HTTP date format. This is used when a single method is required to convert a long data value in milliseconds to a HTTP date value.
      Parameters:
      date - the date specified as a long of milliseconds
      Returns:
      the date represented in the HTTP date format RFC 1123
    • init

      protected void init()
      This is used to reset the date and the buffer variables for this DateParser. Every in is set to the value of 0.
      Specified by:
      init in class Parser
    • parse

      protected void parse()
      This is used to parse the contents of the buf. This checks the fourth char of the buffer to see what it contains. Invariably a date format belonging to RFC 1123 will have a ',' character in position 4, a date format belonging to asctime will have a ' ' character in position 4 and if neither of these characters are found at position 4 then it is assumed that the date is in the RFC 850 fromat, however it may not be.
      Specified by:
      parse in class Parser
    • toLong

      public long toLong()
      This returns the date in as a long, given the exact time this will use the java.util.Date to parse this date into a long. The GregorianCalendar uses the method getTime which produces the Date object from this the getTime returns the long
      Returns:
      the date parsed as a long
    • toString

      public String toString()
      This prints the date in the format of a RFC 1123 date. Example
       Tue, 02 Jun 1982 23:59:59 GMT
       
      . This uses a StringBuffer to accumulate the various Strings/ints to form the resulting date value. The resulting date value is the one required by RFC 2616.

      The HTTP date must be in the form of RFC 1123. The hours, minutes and seconds are appended with the 0 character if they are less than 9 i.e. if they do not have two digits.

      Overrides:
      toString in class Object
      Returns:
      the date in RFC 1123 format