Class DateParser
- java.lang.Object
-
- org.simpleframework.common.parse.Parser
-
- org.simpleframework.http.parse.DateParser
-
public class DateParser extends Parser
This is used to create aParserfor 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.currentTimeMillismethod. This has a parse method for alongwhich will do the same as theparse(String). Once the date has been parsed there are two methods that allow the date to be represented, thetoLongmethod converts the date to alongand thetoStringmethod will convert the date into aString.This produces the same string as the
SimpleDateFormat.formatusing 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
Fields Modifier and Type Field Description private intdayRepresents the decimal value of the date such as 18.private inthourRepresents the decimal value of the hour such as 24.private intminsRepresents the decimal value of the minute.private intmonthUsed as an index into the months array to get the month.private static java.lang.String[]MONTHSContains the possible months in the year for HTTP-date.private intsecsRepresents the decimal value for the second.private intweekdayUsed as an index into the weekdays array to get the weekday.private static java.lang.String[]WEEKDAYSContains the possible days of the week for RFC 850.private static java.lang.String[]WKDAYSContains the possible days of the week for RFC 1123.private intyearRepresents the decimal value of the date such as 1977.private static java.util.TimeZoneZONEEnsure that the time zone for dates if set to GMT.
-
Constructor Summary
Constructors Constructor Description DateParser()The default constructor will create a parser that can parseStrings that contain dates in the form of RFC 1123, RFC 850 or asctime.DateParser(long date)This constructor will conveniently parse thelongargument in the constructor.DateParser(java.lang.String date)This constructor will conveniently parse theStringargument in the constructor.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description private voidasctime()This will parse a date that is in the form of an asctime date.java.lang.Stringconvert(long date)Convenience method used to convert the specified long date in to a HTTP date format.longconvert(java.lang.String date)Convenience method used to convert the specified HTTP date in to a long representing the time.private voiddate(java.lang.StringBuilder builder)This is used to append the date in RFC 1123 format to the given string builder.private voiddate1()This is the date1 format of a date that is used by the RFC 1123 date format.private voiddate2()This is the date2 format of a date that is used by the RFC 850 date format.private voiddate3()This is the date3 format of a date that is used by the asctime date format.private voidday()This is used to parse a consecutive set of digit characters to create the day of the week.private voidhours()This is used to initialize the hour.protected voidinit()This is used to reset the date and the buffer variables for thisDateParser.private voidmins()This is used to initialize the mins.private voidmonth()This is used to read the month of HTTP-date.protected voidparse()This is used to parse the contents of thebuf.voidparse(long date)This is used to extract the date from along.private voidrfc1123()This will parse a date that is in the form of an RFC 1123 date.private voidrfc850()This will parse a date that is in the form of an RFC 850 date.private voidsecs()This is used to initialize the secs.private voidtime()This is used to parse the time for a HTTP-date.private voidtime(java.lang.StringBuilder builder)This is used to append the time in RFC 1123 format to the given string builder.longtoLong()This returns the date in as along, given the exact time this will use thejava.util.Dateto parse this date into along.java.lang.StringtoString()This prints the date in the format of a RFC 1123 date.private voidweekday()This is used to read the week day of HTTP-date.private voidwkday()This is used to read the week day of HTTP-date.private voidyear2()This is used to get the year from a set of digit characters.private voidyear4()This is used to get the year from a set of digit characters.private voidzone(java.lang.StringBuilder builder)This is used to append the time zone to the provided appender.
-
-
-
Field Detail
-
ZONE
private static final java.util.TimeZone ZONE
Ensure that the time zone for dates if set to GMT.
-
WKDAYS
private static final java.lang.String[] WKDAYS
Contains the possible days of the week for RFC 1123.
-
WEEKDAYS
private static final java.lang.String[] WEEKDAYS
Contains the possible days of the week for RFC 850.
-
MONTHS
private static final java.lang.String[] MONTHS
Contains the possible months in the year for HTTP-date.
-
month
private int month
Used as an index into the months array to get the month.
-
year
private int year
Represents the decimal value of the date such as 1977.
-
day
private int day
Represents the decimal value of the date such as 18.
-
weekday
private int weekday
Used as an index into the weekdays array to get the weekday.
-
hour
private int hour
Represents the decimal value of the hour such as 24.
-
mins
private int mins
Represents the decimal value of the minute.
-
secs
private int secs
Represents the decimal value for the second.
-
-
Constructor Detail
-
DateParser
public DateParser()
The default constructor will create a parser that can parseStrings 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 thelongargument 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 theStringargument 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 alongit will construct the RFC 1123 date as required by RFC 2616 sec 3.3.This saves time on parsing a
Stringthat 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 creatingDateobjects.- 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.
-
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:00that 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
private void date(java.lang.StringBuilder builder)
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
private void time(java.lang.StringBuilder builder)
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
private void zone(java.lang.StringBuilder builder)
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.Dateto parse this date into along. TheGregorianCalendaruses the methodgetTimewhich produces theDateobject from this thegetTimereturns 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 aStringBufferto accumulate the variousStrings/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:
toStringin classjava.lang.Object- Returns:
- the date in RFC 1123 format
-
-