Top Description Fields Constructors Methods
com.rometools.rome.io.impl

public Class DateParser

extends Object
Class Inheritance
Imports
java.text.DateFormat, .ParsePosition, .SimpleDateFormat, java.util.Arrays, .Date, .List, .Locale, .TimeZone

A helper class that parses Dates out of Strings with date time in RFC822 and W3CDateTime formats plus the variants Atom (0.3) and RSS (0.9, 0.91, 0.92, 0.93, 0.94, 1.0 and 2.0) specificators added to those formats.

It uses the JDK java.text.SimpleDateFormat class attemtping the parse using a mask for each one of the possible formats.

Field Summary

Modifier and TypeField and Description
private static String[]
private static final String[]
masks

The masks used to validate and parse the input to this Atom date.

private static final String[]
private static final String[]

Constructor Summary

AccessConstructor and Description
private
DateParser()

Private constructor to avoid DateParser instances creation.

Method Summary

Modifier and TypeMethod and Description
private static String
public static String

Returns:

the RFC822 represented by the given Date It returns null if it was not possible to parse the date.
formatRFC822
(final Date
Date to parse
date
,
final Locale locale)

create a RFC822 representation of a date.

public static String

Returns:

the W3C Date Time represented by the given Date It returns null if it was not possible to parse the date.
formatW3CDateTime
(final Date
Date to parse
date
,
final Locale locale)

create a W3C Date Time representation of a date.

public static Date

Returns:

the Date represented by the given W3C date-time string. It returns null if it was not possible to parse the given string into a Date.
parseDate
(final String
string to parse for a date.
sDate
,
final Locale locale)

Parses a Date out of a String with a date in W3C date-time format or in a RFC822 format.

public static Date

Returns:

the Date represented by the given RFC822 string. It returns null if it was not possible to parse the given string into a Date.
parseRFC822
(String
string to parse for a date.
sDate
,
final Locale locale)

Parses a Date out of a String with a date in RFC822 format.

private static Date

Returns:

the Date represented by the given string using one of the given masks. It returns null if it was not possible to parse the the string with any of the masks.
parseUsingMask
(final String[]
array of masks to use for parsing the string
masks
,
String
string to parse for a date.
sDate
,
final Locale locale)

Parses a Date out of a string using an array of masks.

public static Date

Returns:

the Date represented by the given W3C date-time string. It returns null if it was not possible to parse the given string into a Date.
parseW3CDateTime
(String
string to parse for a date.
sDate
,
final Locale locale)

Parses a Date out of a String with a date in W3C date-time format.

private static String
replaceLastOccurrence(String original, String target, String replacement)

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

ADDITIONAL_MASKSback to summary
private static String[] ADDITIONAL_MASKS
masksback to summary
private static final String[] masks

The masks used to validate and parse the input to this Atom date. These are a lot more forgiving than what the Atom spec allows. The forms that are invalid according to the spec are indicated.

Annotations
@SuppressWarnings:unused
RFC822_MASKSback to summary
private static final String[] RFC822_MASKS
W3CDATETIME_MASKSback to summary
private static final String[] W3CDATETIME_MASKS

Constructor Detail

DateParserback to summary
private DateParser()

Private constructor to avoid DateParser instances creation.

Method Detail

convertUnsupportedTimeZonesback to summary
private static String convertUnsupportedTimeZones(String sDate)
formatRFC822back to summary
public static String formatRFC822(final Date date, final Locale locale)

create a RFC822 representation of a date.

Refer to the java.text.SimpleDateFormat javadocs for details on the format of each element.

Parameters
date:Date

Date to parse

Returns:String

the RFC822 represented by the given Date It returns null if it was not possible to parse the date.

formatW3CDateTimeback to summary
public static String formatW3CDateTime(final Date date, final Locale locale)

create a W3C Date Time representation of a date.

Refer to the java.text.SimpleDateFormat javadocs for details on the format of each element.

Parameters
date:Date

Date to parse

Returns:String

the W3C Date Time represented by the given Date It returns null if it was not possible to parse the date.

parseDateback to summary
public static Date parseDate(final String sDate, final Locale locale)

Parses a Date out of a String with a date in W3C date-time format or in a RFC822 format.

Parameters
sDate:String

string to parse for a date.

Returns:Date

the Date represented by the given W3C date-time string. It returns null if it was not possible to parse the given string into a Date.

parseRFC822back to summary
public static Date parseRFC822(String sDate, final Locale locale)

Parses a Date out of a String with a date in RFC822 format.

It parsers the following formats:

  • "EEE, dd MMM yyyy HH:mm:ss z"
  • "EEE, dd MMM yyyy HH:mm z"
  • "EEE, dd MMM yy HH:mm:ss z"
  • "EEE, dd MMM yy HH:mm z"
  • "dd MMM yyyy HH:mm:ss z"
  • "dd MMM yyyy HH:mm z"
  • "dd MMM yy HH:mm:ss z"
  • "dd MMM yy HH:mm z"

Refer to the java.text.SimpleDateFormat javadocs for details on the format of each element.

Parameters
sDate:String

string to parse for a date.

Returns:Date

the Date represented by the given RFC822 string. It returns null if it was not possible to parse the given string into a Date.

parseUsingMaskback to summary
private static Date parseUsingMask(final String[] masks, String sDate, final Locale locale)

Parses a Date out of a string using an array of masks.

It uses the masks in order until one of them succedes or all fail.

Parameters
masks:String[]

array of masks to use for parsing the string

sDate:String

string to parse for a date.

Returns:Date

the Date represented by the given string using one of the given masks. It returns null if it was not possible to parse the the string with any of the masks.

parseW3CDateTimeback to summary
public static Date parseW3CDateTime(String sDate, final Locale locale)

Parses a Date out of a String with a date in W3C date-time format.

It parsers the following formats:

  • "yyyy-MM-dd'T'HH:mm:ssz"
  • "yyyy-MM-dd'T'HH:mmz"
  • "yyyy-MM-dd"
  • "yyyy-MM"
  • "yyyy"

Refer to the java.text.SimpleDateFormat javadocs for details on the format of each element.

Parameters
sDate:String

string to parse for a date.

Returns:Date

the Date represented by the given W3C date-time string. It returns null if it was not possible to parse the given string into a Date.

replaceLastOccurrenceback to summary
private static String replaceLastOccurrence(String original, String target, String replacement)