Author: ltheussl Date: Tue Jun 2 08:33:19 2009 New Revision: 780966 URL: http://svn.apache.org/viewvc?rev=780966&view=rev Log: Add a common method for parsing dates
Modified: maven/doxia/doxia/trunk/doxia-core/src/main/java/org/apache/maven/doxia/util/DoxiaUtils.java maven/doxia/doxia/trunk/doxia-core/src/test/java/org/apache/maven/doxia/util/DoxiaUtilsTest.java Modified: maven/doxia/doxia/trunk/doxia-core/src/main/java/org/apache/maven/doxia/util/DoxiaUtils.java URL: http://svn.apache.org/viewvc/maven/doxia/doxia/trunk/doxia-core/src/main/java/org/apache/maven/doxia/util/DoxiaUtils.java?rev=780966&r1=780965&r2=780966&view=diff ============================================================================== --- maven/doxia/doxia/trunk/doxia-core/src/main/java/org/apache/maven/doxia/util/DoxiaUtils.java (original) +++ maven/doxia/doxia/trunk/doxia-core/src/main/java/org/apache/maven/doxia/util/DoxiaUtils.java Tue Jun 2 08:33:19 2009 @@ -20,6 +20,12 @@ */ import java.io.UnsupportedEncodingException; + +import java.text.ParseException; +import java.text.ParsePosition; +import java.text.SimpleDateFormat; + +import java.util.Date; import java.util.Locale; /** @@ -281,6 +287,61 @@ return true; } + private static final SimpleDateFormat DATE_PARSER = new SimpleDateFormat(); + private static final ParsePosition DATE_PARSE_POSITION = new ParsePosition( 0 ); + private static final String[] DATE_PATTERNS = new String[] + { + "yyyy-MM-dd", "yyyy/MM/dd", "yyyyMMdd", "yyyy", "dd.MM.yyyy", "dd MMM yyyy", + "dd MMM. yyyy", "MMMM yyyy", "MMM. dd, yyyy", "MMM. yyyy", "MMMM dd, yyyy", + "MMM d, ''yy", "MMM. ''yy", "MMMM ''yy" + }; + + /** + * <p>Parses a string representing a date by trying different date patterns.</p> + * + * <p>The following date patterns are tried (in the given order):</p> + * + * <pre>"yyyy-MM-dd", "yyyy/MM/dd", "yyyyMMdd", "yyyy", "dd.MM.yyyy", "dd MMM yyyy", + * "dd MMM. yyyy", "MMMM yyyy", "MMM. dd, yyyy", "MMM. yyyy", "MMMM dd, yyyy", + * "MMM d, ''yy", "MMM. ''yy", "MMMM ''yy"</pre> + * + * <p>A parse is only sucessful if it parses the whole of the input string. + * If no parse patterns match, a ParseException is thrown.</p> + * + * <p>As a special case, the strings <code>"today"</code> and <code>"now"</code> + * (ignoring case) return the current date.</p> + * + * @param str the date to parse, not null. + * @return the parsed date, or the current date if the input String (ignoring case) was + * <code>"today"</code> or <code>"now"</code>. + * @throws ParseException if no pattern matches. + * + * @since 1.1.1. + */ + public static Date parseDate( String str ) + throws ParseException + { + if ( "today".equals( str.toLowerCase( Locale.ENGLISH ) ) + || "now".equals( str.toLowerCase( Locale.ENGLISH ) ) ) + { + return new Date(); + } + + for ( int i = 0; i < DATE_PATTERNS.length; i++ ) + { + DATE_PARSER.applyPattern( DATE_PATTERNS[i] ); + DATE_PARSE_POSITION.setIndex( 0 ); + final Date date = DATE_PARSER.parse( str, DATE_PARSE_POSITION ); + + if ( date != null && DATE_PARSE_POSITION.getIndex() == str.length() ) + { + return date; + } + } + + throw new ParseException( "Unable to parse date: " + str, -1 ); + } + // // private // Modified: maven/doxia/doxia/trunk/doxia-core/src/test/java/org/apache/maven/doxia/util/DoxiaUtilsTest.java URL: http://svn.apache.org/viewvc/maven/doxia/doxia/trunk/doxia-core/src/test/java/org/apache/maven/doxia/util/DoxiaUtilsTest.java?rev=780966&r1=780965&r2=780966&view=diff ============================================================================== --- maven/doxia/doxia/trunk/doxia-core/src/test/java/org/apache/maven/doxia/util/DoxiaUtilsTest.java (original) +++ maven/doxia/doxia/trunk/doxia-core/src/test/java/org/apache/maven/doxia/util/DoxiaUtilsTest.java Tue Jun 2 08:33:19 2009 @@ -19,6 +19,11 @@ * under the License. */ +import java.text.ParseException; + +import java.util.Date; +import java.util.GregorianCalendar; + import org.codehaus.plexus.PlexusTestCase; /** @@ -184,4 +189,54 @@ assertTrue( DoxiaUtils.isValidId( "index.html" ) ); assertFalse( DoxiaUtils.isValidId( "TheuÃl" ) ); } + + /** + * Verify the expected results. + */ + public void testParseDate() + { + final int year = 1973; + final int month = 1; + final int day = 27; + + try + { + final Date feb27 = new GregorianCalendar( year, month, day ).getTime(); + assertEquals( feb27, DoxiaUtils.parseDate( "27.02.1973" ) ); + assertEquals( feb27, DoxiaUtils.parseDate( "27. 02. 1973" ) ); + assertEquals( feb27, DoxiaUtils.parseDate( "1973-02-27" ) ); + assertEquals( feb27, DoxiaUtils.parseDate( "1973/02/27" ) ); + assertEquals( feb27, DoxiaUtils.parseDate( "27 Feb 1973" ) ); + assertEquals( feb27, DoxiaUtils.parseDate( "27 Feb. 1973" ) ); + assertEquals( feb27, DoxiaUtils.parseDate( "Feb. 27, 1973" ) ); + assertEquals( feb27, DoxiaUtils.parseDate( "Feb 27, '73" ) ); + assertEquals( feb27, DoxiaUtils.parseDate( "February 27, 1973" ) ); + assertEquals( feb27, DoxiaUtils.parseDate( "19730227" ) ); + + assertEquals( new GregorianCalendar( year, 0, 1 ).getTime(), DoxiaUtils.parseDate( "1973" ) ); + + final Date feb1 = new GregorianCalendar( year, 1, 1 ).getTime(); + assertEquals( feb1, DoxiaUtils.parseDate( "February 1973" ) ); + assertEquals( feb1, DoxiaUtils.parseDate( "Feb. 1973" ) ); + assertEquals( feb1, DoxiaUtils.parseDate( "February '73" ) ); + assertEquals( feb1, DoxiaUtils.parseDate( "Feb. '73" ) ); + + assertNotNull( DoxiaUtils.parseDate( "Today" ) ); + assertNotNull( DoxiaUtils.parseDate( "NOW" ) ); + } + catch ( ParseException ex ) + { + fail( ex.getMessage() ); + } + + try + { + DoxiaUtils.parseDate( "yesterday" ).getTime(); + fail(); + } + catch ( ParseException ex ) + { + assertNotNull( ex ); + } + } }