Skip to content

Commit

Permalink
Convert custom supported date format strings.
Browse files Browse the repository at this point in the history
  • Loading branch information
dkocher committed Jan 10, 2025
1 parent f9fe96d commit 49a37c9
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 28 deletions.
48 changes: 20 additions & 28 deletions src/main/java/com/github/sardine/util/SardineUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.*;

/**
Expand All @@ -36,16 +38,16 @@ public final class SardineUtil
{
private SardineUtil() {}

private final static String[] SUPPORTED_DATE_FORMATS = new String[]{
"yyyy-MM-dd'T'HH:mm:ss'Z'",
"EEE, dd MMM yyyy HH:mm:ss zzz",
"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'",
"yyyy-MM-dd'T'HH:mm:ssZ",
"EEE MMM dd HH:mm:ss zzz yyyy",
"EEEEEE, dd-MMM-yy HH:mm:ss zzz",
"EEE MMMM d HH:mm:ss yyyy"};
private final static String[] SUPPORTED_DATE_FORMATS = new String[]{
"yyyy-MM-dd'T'HH:mm:ss'Z'",
"EEE, dd MMM yyyy HH:mm:ss zzz", // RFC1123
"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'",
"yyyy-MM-dd'T'HH:mm:ssZ",
"EEE MMM dd HH:mm:ss zzz yyyy",
"EEEE, dd-MMM-yy HH:mm:ss zzz",
"EEE MMMM d HH:mm:ss yyyy"};

/**
/**
* Default namespace prefix
*/
public static final String CUSTOM_NAMESPACE_PREFIX = "s";
Expand Down Expand Up @@ -84,19 +86,6 @@ private SardineUtil() {}
}
}

/**
* Date formats using for Date parsing.
*/
private static final List<ThreadLocal<SimpleDateFormat>> DATETIME_FORMATS;

static {
List<ThreadLocal<SimpleDateFormat>> l = new ArrayList<ThreadLocal<SimpleDateFormat>>(SUPPORTED_DATE_FORMATS.length);
for (int i = 0; i<SUPPORTED_DATE_FORMATS.length; i++){
l.add(new ThreadLocal<SimpleDateFormat>());
}
DATETIME_FORMATS = Collections.unmodifiableList(l);
}

/**
* Loops over all the possible date formats and tries to find the right one.
*
Expand All @@ -109,7 +98,13 @@ public static Date parseDate(String value)
{
return null;
}
return DateUtils.parseDate(value, SUPPORTED_DATE_FORMATS);
final Instant parsedInstant = DateUtils.parseDate(value, Arrays.stream(SUPPORTED_DATE_FORMATS)
.map(pattern -> DateTimeFormatter.ofPattern(pattern, Locale.US).withZone(ZoneId.of("GMT"))).toArray(DateTimeFormatter[]::new));
if (null == parsedInstant)
{
return null;
}
return Date.from(parsedInstant);
}

@SuppressWarnings("unchecked")
Expand Down Expand Up @@ -163,10 +158,7 @@ public static <T> T unmarshal(InputStream in) throws IOException
catch (JAXBException e)
{
// Server does not return any valid WebDAV XML that matches our JAXB context
IOException failure = new IOException("Not a valid DAV response");
// Backward compatibility
failure.initCause(e);
throw failure;
throw new IOException("Not a valid DAV response", e);
}
finally
{
Expand Down
1 change: 1 addition & 0 deletions src/test/java/com/github/sardine/SardineUtilTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public class SardineUtilTest
@Test
public void testParseDate() throws Exception
{
assertNotNull(SardineUtil.parseDate("1970-01-01T12:00:00Z"));
assertNotNull(SardineUtil.parseDate("2007-07-16T13:35:49Z"));
assertNotNull(SardineUtil.parseDate("Mon, 16 Jul 2007 13:35:49 GMT"));
}
Expand Down

0 comments on commit 49a37c9

Please sign in to comment.