Class DateParser
- java.lang.Object
-
- org.simpleframework.common.parse.Parser
-
- org.simpleframework.http.parse.DateParser
-
public class DateParser extends org.simpleframework.common.parse.Parser
This is used to create aParser
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 areSun, 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
'sSystem.currentTimeMillis
method. This has a parse method for along
which will do the same as theparse(String)
. Once the date has been parsed there are two methods that allow the date to be represented, thetoLong
method converts the date to along
and thetoString
method will convert the date into aString
.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
-
-
Constructor Summary
Constructors Constructor Description 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.DateParser(long date)
This constructor will conveniently parse thelong
argument in the constructor.DateParser(java.lang.String date)
This constructor will conveniently parse theString
argument in the constructor.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description java.lang.String
convert(long date)
Convenience method used to convert the specified long date in to a HTTP date format.long
convert(java.lang.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 thisDateParser
.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
.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
.java.lang.String
toString()
This prints the date in the format of a RFC 1123 date.
-
-
-
Constructor Detail
-
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
public DateParser(java.lang.String date)
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 Detail
-
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
public long convert(java.lang.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 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
public java.lang.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 thisDateParser
. Every in is set to the value of 0.- Specified by:
init
in classorg.simpleframework.common.parse.Parser
-
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.- Specified by:
parse
in classorg.simpleframework.common.parse.Parser
-
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
public java.lang.String 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.
- Overrides:
toString
in classjava.lang.Object
- Returns:
- the date in RFC 1123 format
-
-