Skip to content

Commit

Permalink
Merge pull request #32 from ruhan1/master-add-dateformat
Browse files Browse the repository at this point in the history
Support more datetime format
  • Loading branch information
ruhan1 authored Feb 4, 2018
2 parents c561f53 + 8e897c4 commit f4b8058
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 8 deletions.
32 changes: 25 additions & 7 deletions rwx/src/main/java/org/commonjava/rwx/vocab/ValueType.java
Original file line number Diff line number Diff line change
Expand Up @@ -169,16 +169,34 @@ public String toString( final Object value )
DATETIME( new ValueCoercion( "DATETIME-to-String (" + DATETIME_FORMAT + ")" )
{
@Override
public Object fromString( final String value ) throws CoercionException
public Object fromString( String value ) throws CoercionException
{
try
if ( value == null )
{
String val = value == null ? null : value.trim();
return val == null ? null : new SimpleDateFormat( DATETIME_FORMAT ).parse( value );
return null;
}
catch ( final ParseException e )
value = value.trim();
Date obj = null;
ParseException exception = null;
for ( String format : DATETIME_FORMAT )
{
try
{
obj = new SimpleDateFormat( format ).parse( value );
break;
}
catch ( final ParseException e )
{
exception = e;
}
}
if ( obj != null )
{
return obj;
}
else
{
throw new CoercionException( "Cannot parse date: '" + value + "'.", e );
throw new CoercionException( "Cannot parse date: '" + value + "'.", exception );
}
}

Expand All @@ -187,7 +205,7 @@ public String toString( final Object value ) throws CoercionException
{
try
{
return value == null ? null : new SimpleDateFormat( DATETIME_FORMAT ).format( (Date) value );
return value == null ? null : new SimpleDateFormat( DATETIME_FORMAT[0] ).format( (Date) value );
}
catch ( final ClassCastException e )
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,6 @@ public class XmlRpcConstants

public static final String NIL = "nil";

public static final String DATETIME_FORMAT = "yyyyMMdd'T'HHmmss";
public static final String[] DATETIME_FORMAT = { "yyyyMMdd'T'HHmmss", "yyyyMMdd'T'HH:mm:ss" };

}

0 comments on commit f4b8058

Please sign in to comment.