public class DateParser extends Parser
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.
Constructor and Description |
---|
DateParser()
The default constructor will create a parser that can parse
String s 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. |
DateParser(String date)
This constructor will conveniently parse the
String
argument in the constructor. |
Modifier and Type | Method and Description |
---|---|
String |
convert(long date)
Convenience method used to convert the specified long date in to a
HTTP date format.
|
long |
convert(String date)
Convenience method used to convert the specified HTTP date in to a
long representing the time.
|
protected void |
init()
This is used to reset the date and the buffer variables
for this
DateParser . |
protected void |
parse()
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 |
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 . |
String |
toString()
This prints the date in the format of a RFC 1123 date.
|
public DateParser()
String
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.public DateParser(long date)
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.
date
- the date to be parsedpublic DateParser(String date)
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.
date
- the date to be parsedpublic void parse(long date)
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.
date
- the date to be parsedpublic long convert(String date)
Date
objects.date
- the date specified in on of the HTTP date formatspublic String convert(long date)
date
- the date specified as a long of millisecondsprotected void init()
DateParser
. Every in is set to the
value of 0.protected void parse()
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.public long toLong()
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
long
public String toString()
Tue, 02 Jun 1982 23:59:59 GMT. This uses a
StringBuffer
to accumulate the various
String
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.
Copyright © 2023. All rights reserved.