Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 20 additions & 3 deletions src/main/java/com/box/sdk/BoxDateFormat.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* Contains methods for parsing and formatting dates for use with the Box API.
*/
public final class BoxDateFormat {
private static final ThreadLocal<DateFormat> THREAD_LOCAL_DATE_FORMAT = new ThreadLocal<DateFormat>() {
private static final ThreadLocal<DateFormat> THREAD_LOCAL_DATE_FORMAT_SECONDS = new ThreadLocal<DateFormat>() {
@Override
protected DateFormat initialValue() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssX");
Expand All @@ -19,6 +19,15 @@ protected DateFormat initialValue() {
}
};

private static final ThreadLocal<DateFormat> THREAD_LOCAL_DATE_FORMAT_MILLISECONDS = new ThreadLocal<DateFormat>() {
@Override
protected DateFormat initialValue() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSX");
sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
return sdf;
}
};

private BoxDateFormat() { }

/**
Expand All @@ -28,7 +37,15 @@ private BoxDateFormat() { }
* @throws ParseException if the string cannot be parsed into a valid date.
*/
public static Date parse(String dateString) throws ParseException {
return THREAD_LOCAL_DATE_FORMAT.get().parse(dateString);
try {
return THREAD_LOCAL_DATE_FORMAT_SECONDS.get().parse(dateString);
} catch (ParseException pe) {
try {
return THREAD_LOCAL_DATE_FORMAT_MILLISECONDS.get().parse(dateString);
} catch (ParseException pe2) {
throw pe2;
}
}
}

/**
Expand All @@ -37,7 +54,7 @@ public static Date parse(String dateString) throws ParseException {
* @return a string containing the formatted date.
*/
public static String format(Date date) {
return THREAD_LOCAL_DATE_FORMAT.get().format(date);
return THREAD_LOCAL_DATE_FORMAT_SECONDS.get().format(date);
}

}
14 changes: 11 additions & 3 deletions src/test/java/com/box/sdk/MetadataTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -296,22 +296,30 @@ public void testMultiSelectMetadataCRUD() {

@Test
@Category(UnitTest.class)
public void getValues() {
public void testGetValues() {
// Run test with various Date formats.
this.getValues("\"2017-10-10T22:10:00-08:00\"", 1507702200000L);
this.getValues("\"2017-10-10T22:10:00.000-08:00\"", 1507702200000L);
this.getValues("\"2017-10-10T22:10:00.123-08:00\"", 1507702200123L);
this.getValues("\"2017-10-10T22:10:00.100-08:00\"", 1507702200100L);
}

public void getValues(String dateString, Long dateLong) {

final String stringValue = "Q1 plans";
final int intValue = 123456;
final String[] arrayValue = new String[2];
arrayValue[0] = "internal";
arrayValue[1] = "internalEng";
Date dateValue = new Date();
dateValue.setTime(1507702200000L);
dateValue.setTime(dateLong);

String json = "{\n"
+ " \"audiences\": [\"internal\", \"internalEng\"],\n"
+ " \"documentType\": \"" + stringValue + "\",\n"
+ " \"competitiveDocument\": \"no\",\n"
+ " \"status\": \"active\",\n"
+ " \"deadline\": \"2017-10-10T22:10:00-08:00\",\n"
+ " \"deadline\": " + dateString + ",\n"
+ " \"capacity\": " + intValue + ",\n"
+ " \"currentState\": \"proposal\",\n"
+ " \"$type\": \"marketingCollateral-d086c908-2498-4d3e-8a1f-01e82bfc2abe\",\n"
Expand Down