net.sf.mmm.util.date.base
Class Iso8601UtilImpl

java.lang.Object
  extended by net.sf.mmm.util.date.base.Iso8601UtilImpl
All Implemented Interfaces:
Iso8601Util

@Singleton
@Named
public final class Iso8601UtilImpl
extends Object
implements Iso8601Util

This is the implementation of the Iso8601Util interface. It does NOT use SimpleDateFormat. All methods of this class are fast and thread-safe.

Since:
1.0.0
Author:
Joerg Hohwiller (hohwille at users.sourceforge.net)
See Also:
getInstance()

Field Summary
private static Iso8601UtilImpl instance
          This is the singleton instance of this Iso8601UtilImpl.
private static int MAX_DAY_OF_MONTH
          The maximum day of the month.
private static int MAX_MONTH
          The maximum month of the year.
private static TimeZone TZ_UTC
          The UTC TimeZone.
private static String UTC_ID
          The ID for UTC (Coordinated Universal Time).
 
Fields inherited from interface net.sf.mmm.util.date.api.Iso8601Util
PATTERN_ALL, PATTERN_STRING_ALL, PATTERN_STRING_DATE, PATTERN_STRING_TIME, PATTERN_STRING_TIMEZONE
 
Constructor Summary
Iso8601UtilImpl()
          The constructor.
 
Method Summary
 String formatDate(Calendar calendar)
          This method formats the given calendar as a date in the format "yyyy-MM-dd" according to ISO 8601.
 String formatDate(Calendar calendar, boolean extended)
          This method formats the given calendar as a date in the format "yyyy-MM-dd" according to ISO 8601.
 void formatDate(Calendar calendar, boolean extended, Appendable buffer)
          This method formats the given calendar as a date according to ISO 8601.
 String formatDate(Date date)
          This method formats the given date in the format "yyyy-MM-dd" in GMT according to ISO 8601.
 String formatDateTime(Calendar calendar)
          This method formats the given calendar as a date and time in the format "yyyy-MM-ddTHH:mm:ss±hh:mm" according to ISO 8601.
 String formatDateTime(Calendar calendar, boolean extendedDate, boolean extendedTime, boolean extendedTimezone)
          This method formats the given calendar as a date and time in the format "yyyy-MM-ddTHH:mm:ss±hh:mm" according to ISO 8601.
 void formatDateTime(Calendar calendar, boolean extendedDate, boolean extendedTime, boolean extendedTimezone, Appendable buffer)
          This method formats the given calendar as a date and time in the format "yyyy-MM-ddTHH:mm:ss±hh:mm" according to ISO 8601.
 String formatDateTime(Date date)
          This method formats the given date as a date and time in the format "yyyy-MM-ddTHH:mm:ssZ" (UTC) according to ISO 8601.
 void formatTime(Calendar calendar, boolean extended, Appendable buffer)
          This method formats the given calendar as time according to ISO 8601.
 void formatTimeZone(Calendar calendar, boolean extended, Appendable buffer)
          This method formats the given timezone according to ISO 8601.
 void formatTimeZone(int timezoneOffset, boolean extended, Appendable buffer)
          This method formats the given timezone according to ISO 8601.
static Iso8601UtilImpl getInstance()
          This method gets the singleton instance of this Iso8601UtilImpl.
 Calendar parseCalendar(String date)
          This method parses the given string date according to ISO 8601.
 void parseCalendar(String date, Calendar calendar)
          This method parses the given date according to ISO 8601 using the given calendar.
 Date parseDate(String date)
          This method parses the given string date according to ISO 8601.
private  int[] parseTime(CharSequenceScanner scanner)
          This method parses the time (or timezone offset) from the given parser.
private  void parseTime(CharSequenceScanner scanner, Calendar calendar, int year, int month, int day)
          This method parses the time (and timezone) from the given parser and sets it to the given calendar including year, month and date.
private  TimeZone parseTimezone(CharSequenceScanner scanner)
          This method parses the timezone from the given parser.
private  int read2Digits(CharSequenceScanner scanner)
          This method reads two digits from the given scanner.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

MAX_DAY_OF_MONTH

private static final int MAX_DAY_OF_MONTH
The maximum day of the month.

See Also:
Constant Field Values

MAX_MONTH

private static final int MAX_MONTH
The maximum month of the year.

See Also:
Constant Field Values

instance

private static Iso8601UtilImpl instance
This is the singleton instance of this Iso8601UtilImpl. Instead of declaring the methods static, we declare this static instance what gives the same way of access while still allowing a design for extension by inheriting from this class.


UTC_ID

private static final String UTC_ID
The ID for UTC (Coordinated Universal Time).

See Also:
Constant Field Values

TZ_UTC

private static final TimeZone TZ_UTC
The UTC TimeZone.

Constructor Detail

Iso8601UtilImpl

public Iso8601UtilImpl()
The constructor.

Method Detail

getInstance

public static Iso8601UtilImpl getInstance()
This method gets the singleton instance of this Iso8601UtilImpl.
This design is the best compromise between easy access (via this indirection you have direct, static access to all offered functionality) and IoC-style design which allows extension and customization.
For IoC usage, simply ignore all static getInstance() methods and construct new instances via the container-framework of your choice (like plexus, pico, springframework, etc.). To wire up the dependent components everything is properly annotated using common-annotations (JSR-250). If your container does NOT support this, you should consider using a better one.

Returns:
the singleton instance.

formatDate

public String formatDate(Date date)
This method formats the given date in the format "yyyy-MM-dd" in GMT according to ISO 8601.

Specified by:
formatDate in interface Iso8601Util
Parameters:
date - is the date to format.
Returns:
the given date as date string.

formatDate

public String formatDate(Calendar calendar)
This method formats the given calendar as a date in the format "yyyy-MM-dd" according to ISO 8601.

Specified by:
formatDate in interface Iso8601Util
Parameters:
calendar - is the date to format.
Returns:
the given calendar as date string.

formatDate

public String formatDate(Calendar calendar,
                         boolean extended)
This method formats the given calendar as a date in the format "yyyy-MM-dd" according to ISO 8601.

Specified by:
formatDate in interface Iso8601Util
Parameters:
calendar - is the date to format.
extended - if false the basic format ("yyyyMMdd") is used, if true the extended format ("yyyy-MM-dd") is used.
Returns:
the given calendar as date string.

formatDate

public void formatDate(Calendar calendar,
                       boolean extended,
                       Appendable buffer)
This method formats the given calendar as a date according to ISO 8601.

Specified by:
formatDate in interface Iso8601Util
Parameters:
calendar - is the date to format.
extended - if false the basic date format ("yyyyMMdd") is used, if true the extended date format ("yyyy-MM-dd") is used.
buffer - is where to append the formatted date.

formatDateTime

public String formatDateTime(Date date)
This method formats the given date as a date and time in the format "yyyy-MM-ddTHH:mm:ssZ" (UTC) according to ISO 8601.

Specified by:
formatDateTime in interface Iso8601Util
Parameters:
date - is the date to format.
Returns:
the given calendar as date string.

formatDateTime

public String formatDateTime(Calendar calendar)
This method formats the given calendar as a date and time in the format "yyyy-MM-ddTHH:mm:ss±hh:mm" according to ISO 8601.

Specified by:
formatDateTime in interface Iso8601Util
Parameters:
calendar - is the date to format.
Returns:
the given calendar as date string.

formatDateTime

public String formatDateTime(Calendar calendar,
                             boolean extendedDate,
                             boolean extendedTime,
                             boolean extendedTimezone)
This method formats the given calendar as a date and time in the format "yyyy-MM-ddTHH:mm:ss±hh:mm" according to ISO 8601.

Specified by:
formatDateTime in interface Iso8601Util
Parameters:
calendar - is the Calendar to format.
extendedDate - if false the basic date format ("yyyyMMdd") is used, if true the extended date format ("yyyy-MM-dd") is used.
extendedTime - if false the basic time format ("HHmmss") is used, if true the extended time format ("HH:mm:ss") is used.
extendedTimezone - if false the basic timezone format ("±HHmm[ss]") is used, if true the extended timezone format ("±HH:mm[:ss]") is used.
Returns:
the given calendar as date string.

formatDateTime

public void formatDateTime(Calendar calendar,
                           boolean extendedDate,
                           boolean extendedTime,
                           boolean extendedTimezone,
                           Appendable buffer)
This method formats the given calendar as a date and time in the format "yyyy-MM-ddTHH:mm:ss±hh:mm" according to ISO 8601.

Specified by:
formatDateTime in interface Iso8601Util
Parameters:
calendar - is the Calendar to format.
extendedDate - if false the basic date format ("yyyyMMdd") is used, if true the extended date format ("yyyy-MM-dd") is used.
extendedTime - if false the basic time format ("HHmmss") is used, if true the extended time format ("HH:mm:ss") is used.
extendedTimezone - if false the basic timezone format ("±HHmm[ss]") is used, if true the extended timezone format ("±HH:mm[:ss]") is used.
buffer - is where to append the formatted date and time.

formatTime

public void formatTime(Calendar calendar,
                       boolean extended,
                       Appendable buffer)
This method formats the given calendar as time according to ISO 8601.

Specified by:
formatTime in interface Iso8601Util
Parameters:
calendar - is the Calendar to format.
extended - if false the basic time format ("HHmmss") is used, if true the extended time format ("HH:mm:ss") is used.
buffer - is where to append the formatted date.

formatTimeZone

public void formatTimeZone(Calendar calendar,
                           boolean extended,
                           Appendable buffer)
This method formats the given timezone according to ISO 8601.

Specified by:
formatTimeZone in interface Iso8601Util
Parameters:
calendar - is the Calendar to format.
extended - - if false the basic timezone format ("±HHmm[ss]") is used, if true the extended timezone format ("±HH:mm[:ss]") is used.
buffer - is where to append the formatted timezone.

formatTimeZone

public void formatTimeZone(int timezoneOffset,
                           boolean extended,
                           Appendable buffer)
This method formats the given timezone according to ISO 8601.

Specified by:
formatTimeZone in interface Iso8601Util
Parameters:
timezoneOffset - is the timezone-offset in milliseconds.
extended - - if false the basic timezone format ("±HHmm[ss]") is used, if true the extended timezone format ("±HH:mm[:ss]") is used.
buffer - is where to append the formatted timezone.

parseDate

public Date parseDate(String date)
This method parses the given string date according to ISO 8601.

Specified by:
parseDate in interface Iso8601Util
Parameters:
date - is the date to parse.
Returns:
the parsed date.

parseCalendar

public Calendar parseCalendar(String date)
This method parses the given string date according to ISO 8601.

Specified by:
parseCalendar in interface Iso8601Util
Parameters:
date - is the date to parse.
Returns:
the parsed date.

read2Digits

private int read2Digits(CharSequenceScanner scanner)
                 throws IllegalDateFormatException
This method reads two digits from the given scanner.

Parameters:
scanner - is the scanner potentially pointing to the digits.
Returns:
-1 if the scanner does NOT point to a digit or the number represented by the two digits consumed from the scanner.
Throws:
IllegalDateFormatException - if the scanner only contained a single digit.

parseTime

private int[] parseTime(CharSequenceScanner scanner)
This method parses the time (or timezone offset) from the given parser. The format is hh[[:]mm[[:]ss]]

Parameters:
scanner - is the parser pointing to the time.
Returns:
an int-array containing the hour, minute and second in that order.

parseTime

private void parseTime(CharSequenceScanner scanner,
                       Calendar calendar,
                       int year,
                       int month,
                       int day)
This method parses the time (and timezone) from the given parser and sets it to the given calendar including year, month and date.

Parameters:
scanner - is the parser pointing to the time or at the end of the string
calendar - is the calendar where the parsed date and time will be set.
year - is the year to set that has already been parsed.
month - is the month to set that has already been parsed (in the range of 1-12).
day - is the day to set that has already been parsed.

parseTimezone

private TimeZone parseTimezone(CharSequenceScanner scanner)
This method parses the timezone from the given parser.

Parameters:
scanner - is the parser pointing to the timezone or at the end of the string
Returns:
the parsed timezone or null if parser already at the end of the string.

parseCalendar

public void parseCalendar(String date,
                          Calendar calendar)
This method parses the given date according to ISO 8601 using the given calendar. If the given date does NOT specify the time or timezone, the values from the given calendar will be kept.

Specified by:
parseCalendar in interface Iso8601Util
Parameters:
date - is the date to parse.
calendar - is the calendar where the parsed date will be set.


Copyright © 2001-2010 mmm-Team. All Rights Reserved.