Class DateParser
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.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate int
Represents the decimal value of the date such as 18.private int
Represents the decimal value of the hour such as 24.private int
Represents the decimal value of the minute.private int
Used as an index into the months array to get the month.private static final String[]
Contains the possible months in the year for HTTP-date.private int
Represents the decimal value for the second.private int
Used as an index into the weekdays array to get the weekday.private static final String[]
Contains the possible days of the week for RFC 850.private static final String[]
Contains the possible days of the week for RFC 1123.private int
Represents the decimal value of the date such as 1977.private static final TimeZone
Ensure that the time zone for dates if set to GMT. -
Constructor Summary
ConstructorsConstructorDescriptionThe default constructor will create a parser that can parseString
s that contain dates in the form of RFC 1123, RFC 850 or asctime.DateParser
(long date) This constructor will conveniently parse thelong
argument in the constructor.DateParser
(String date) This constructor will conveniently parse theString
argument in the constructor. -
Method Summary
Modifier and TypeMethodDescriptionprivate void
asctime()
This will parse a date that is in the form of an asctime date.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.private void
date
(StringBuilder builder) This is used to append the date in RFC 1123 format to the given string builder.private void
date1()
This is the date1 format of a date that is used by the RFC 1123 date format.private void
date2()
This is the date2 format of a date that is used by the RFC 850 date format.private void
date3()
This is the date3 format of a date that is used by the asctime date format.private void
day()
This is used to parse a consecutive set of digit characters to create the day of the week.private void
hours()
This is used to initialize the hour.protected void
init()
This is used to reset the date and the buffer variables for thisDateParser
.private void
mins()
This is used to initialize the mins.private void
month()
This is used to read the month of HTTP-date.protected void
parse()
This is used to parse the contents of thebuf
.void
parse
(long date) This is used to extract the date from along
.private void
rfc1123()
This will parse a date that is in the form of an RFC 1123 date.private void
rfc850()
This will parse a date that is in the form of an RFC 850 date.private void
secs()
This is used to initialize the secs.private void
time()
This is used to parse the time for a HTTP-date.private void
time
(StringBuilder builder) This is used to append the time in RFC 1123 format to the given string builder.long
toLong()
This returns the date in as along
, given the exact time this will use thejava.util.Date
to parse this date into along
.toString()
This prints the date in the format of a RFC 1123 date.private void
weekday()
This is used to read the week day of HTTP-date.private void
wkday()
This is used to read the week day of HTTP-date.private void
year2()
This is used to get the year from a set of digit characters.private void
year4()
This is used to get the year from a set of digit characters.private void
zone
(StringBuilder builder) This is used to append the time zone to the provided appender.
-
Field Details
-
ZONE
Ensure that the time zone for dates if set to GMT. -
WKDAYS
Contains the possible days of the week for RFC 1123. -
WEEKDAYS
Contains the possible days of the week for RFC 850. -
MONTHS
Contains the possible months in the year for HTTP-date. -
month
private int monthUsed as an index into the months array to get the month. -
year
private int yearRepresents the decimal value of the date such as 1977. -
day
private int dayRepresents the decimal value of the date such as 18. -
weekday
private int weekdayUsed as an index into the weekdays array to get the weekday. -
hour
private int hourRepresents the decimal value of the hour such as 24. -
mins
private int minsRepresents the decimal value of the minute. -
secs
private int secsRepresents the decimal value for the second.
-
-
Constructor Details
-
DateParser
public DateParser()The default constructor will create a parser that can parseString
s 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 thelong
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
This constructor will conveniently parse theString
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
-
parse
public void parse(long date) This is used to extract the date from along
. If this method is given the value of the date as along
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
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 creatingDate
objects.- Parameters:
date
- the date specified in on of the HTTP date formats- Returns:
- the date value as a long value in milliseconds
-
convert
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 thisDateParser
. Every in is set to the value of 0. -
parse
protected void parse()This is used to parse the contents of thebuf
. 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. -
rfc1123
private void rfc1123()This will parse a date that is in the form of an RFC 1123 date. This date format is the date format that is to be used with all applications that are HTTP/1.1 compliant. The RFC 1123 date format isrfc1123 = 'wkday "," SP date1 SP time SP GMT'. date1 = '2DIGIT SP month SP 4DIGIT' and finally time = '2DIGIT ":" 2DIGIT ":" 2DIGIT'.
-
rfc850
private void rfc850()This will parse a date that is in the form of an RFC 850 date. This date format is the date format that is to be used with some applications that are HTTP/1.0 compliant. The RFC 1123 date format isrfc850 = 'weekday "," SP date2 SP time SP GMT'. date2 = '2DIGIT "-" month "-" 2DIGIT' and finally time = '2DIGIT ":" 2DIGIT ":" 2DIGIT'.
-
asctime
private void asctime()This will parse a date that is in the form of an asctime date. This date format is the date format that is to be used with some applications that are HTTP/1.0 compliant. The RFC 1123 date format isasctime = 'weekday SP date3 SP time SP 4DIGIT'. date3 = 'month SP (2DIGIT | (SP 1DIGIT))' and time = '2DIGIT ":" 2DIGIT ":" 2DIGIT'.
-
date1
private void date1()This is the date1 format of a date that is used by the RFC 1123 date format. This date isdate1 = '2DIGIT SP month SP 4DIGIT'. example '02 Jun 1982'.
-
date2
private void date2()This is the date2 format of a date that is used by the RFC 850 date format. This date isdate2 = '2DIGIT "-" month "-" 2DIGIT' example '02-Jun-82'.
-
date3
private void date3()This is the date3 format of a date that is used by the asctime date format. This date isdate3 = 'month SP (2DIGIT | (SP 1DIGIT))' example 'Jun 2'.
-
day
private void day()This is used to parse a consecutive set of digit characters to create the day of the week. This will tolerate a space on front of the digits thiswill allow all date formats including asctime to use this to get the day. This may parse more than 2 digits, however if there are more than 2 digits the date format is incorrect anyway. -
year2
private void year2()This is used to get the year from a set of digit characters. This is used to parse years that are of the form of 2 digits (e.g 82) however this will assume that any dates that are in 2 digit format are dates for the 2000 th milleneum so 01 will be 2001.This may parse more than 2 digits but if there are more than 2 digits in a row then the date format is incorrect anyway.
-
year4
private void year4()This is used to get the year from a set of digit characters. This is used to parse years that are of the form of 4 digits (e.g 1982).This may parse more than 4 digits but if there are more than 2 digits in a row then the date format is incorrect anyway.
-
time
private void time()This is used to parse the time for a HTTP-date. The time for a HTTP-date is in the form00:00:00
that istime = '2DIGIT ":" 2DIGIT ":" 2DIGIT' so this will read only a time of that form, although this will parse time = '2DIGIT CHAR 2DIGIT CHAR 2DIGIT'.
-
hours
private void hours()This is used to initialize the hour. This will read a consecutive sequence of digit characters and convert them into a decimal number to represent the hour that this date represents.This may parse more than 2 digits but if there are more than 2 digits the date is already incorrect.
-
mins
private void mins()This is used to initialize the mins. This will read a consecutive sequence of digit characters and convert them into a decimal number to represent the mins that this date represents.This may parse more than 2 digits but if there are more than 2 digits the date is already incorrect.
-
secs
private void secs()This is used to initialize the secs. This will read a consecutive sequence of digit characters and convert them into a decimal number to represent the secs that this date represents.This may parse more than 2 digits but if there are more than 2 digits the date is already incorrect
-
wkday
private void wkday()This is used to read the week day of HTTP-date. The shorthand day (e.g Mon for Monday) is used by the RFC 1123 and asctime date formats. This will simply try to read each day from the buffer, when the day is read successfully then the index of that day is saved. -
weekday
private void weekday()This is used to read the week day of HTTP-date. This format is used by the RFC 850 date format. This will simply try to read each day from the buffer, when the day is read successfully then the index of that day is saved. -
month
private void month()This is used to read the month of HTTP-date. This will simply try to read each month from the buffer, when the month is read successfully then the index of that month is saved. -
date
This is used to append the date in RFC 1123 format to the given string builder. This will append the date and a trailing space character to the buffer. Dates like the following are appended.Tue, 02 Jun 1982
. For performance reasons a string builder is used. This avoids an unneeded synchronization caused by the string buffers.- Parameters:
builder
- this is the builder to append the date to
-
time
This is used to append the time in RFC 1123 format to the given string builder. This will append the time and a trailing space character to the buffer. Times like the following are appended.23:59:59
. For performance reasons a string builder is used. This avoids an unneeded synchronization caused by the string buffers.- Parameters:
builder
- this is the builder to write the time to
-
zone
This is used to append the time zone to the provided appender. For HTTP the dates should always be in GMT format. So this will simply append the "GMT" string to the end of the builder.- Parameters:
builder
- this builder to append the time zone to
-
toLong
public long toLong()This returns the date in as along
, given the exact time this will use thejava.util.Date
to parse this date into along
. TheGregorianCalendar
uses the methodgetTime
which produces theDate
object from this thegetTime
returns thelong
- Returns:
- the date parsed as a
long
-
toString
This prints the date in the format of a RFC 1123 date. ExampleTue, 02 Jun 1982 23:59:59 GMT
. This uses aStringBuffer
to accumulate the variousString
s/int
s 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.
-