Skip to content

Commit f4b8058

Browse files
authored
Merge pull request #32 from ruhan1/master-add-dateformat
Support more datetime format
2 parents c561f53 + 8e897c4 commit f4b8058

File tree

2 files changed

+26
-8
lines changed

2 files changed

+26
-8
lines changed

rwx/src/main/java/org/commonjava/rwx/vocab/ValueType.java

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -169,16 +169,34 @@ public String toString( final Object value )
169169
DATETIME( new ValueCoercion( "DATETIME-to-String (" + DATETIME_FORMAT + ")" )
170170
{
171171
@Override
172-
public Object fromString( final String value ) throws CoercionException
172+
public Object fromString( String value ) throws CoercionException
173173
{
174-
try
174+
if ( value == null )
175175
{
176-
String val = value == null ? null : value.trim();
177-
return val == null ? null : new SimpleDateFormat( DATETIME_FORMAT ).parse( value );
176+
return null;
178177
}
179-
catch ( final ParseException e )
178+
value = value.trim();
179+
Date obj = null;
180+
ParseException exception = null;
181+
for ( String format : DATETIME_FORMAT )
182+
{
183+
try
184+
{
185+
obj = new SimpleDateFormat( format ).parse( value );
186+
break;
187+
}
188+
catch ( final ParseException e )
189+
{
190+
exception = e;
191+
}
192+
}
193+
if ( obj != null )
194+
{
195+
return obj;
196+
}
197+
else
180198
{
181-
throw new CoercionException( "Cannot parse date: '" + value + "'.", e );
199+
throw new CoercionException( "Cannot parse date: '" + value + "'.", exception );
182200
}
183201
}
184202

@@ -187,7 +205,7 @@ public String toString( final Object value ) throws CoercionException
187205
{
188206
try
189207
{
190-
return value == null ? null : new SimpleDateFormat( DATETIME_FORMAT ).format( (Date) value );
208+
return value == null ? null : new SimpleDateFormat( DATETIME_FORMAT[0] ).format( (Date) value );
191209
}
192210
catch ( final ClassCastException e )
193211
{

rwx/src/main/java/org/commonjava/rwx/vocab/XmlRpcConstants.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,6 @@ public class XmlRpcConstants
4444

4545
public static final String NIL = "nil";
4646

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

4949
}

0 commit comments

Comments
 (0)