use yyyy-mm-dd formatter when converting date to string

This commit is contained in:
Jacky 2018-05-12 20:19:55 -04:00
parent cf5c884ca0
commit 8fe27eff58
3 changed files with 9 additions and 6 deletions

View File

@ -55,7 +55,7 @@ public class ToString implements Function {
Object o2 = args[1]; Object o2 = args[1];
if (o1 instanceof OffsetDateTime) { if (o1 instanceof OffsetDateTime) {
OffsetDateTime odt = (OffsetDateTime)o1; OffsetDateTime odt = (OffsetDateTime)o1;
return odt.format(DateTimeFormatter.ISO_INSTANT); return odt.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
} else if (o1 instanceof Number) { } else if (o1 instanceof Number) {
return String.format((String) o2, (Number) o1); return String.format((String) o2, (Number) o1);
} }

View File

@ -6,7 +6,7 @@ import java.time.format.DateTimeFormatter;
public class StringUtils { public class StringUtils {
/** /**
* String formatting method that knows how to format dates (using the defaul locale's date formatter) * String formatting method that knows how to format dates (using the default locale's date formatter)
* @param o object to be converted to a string * @param o object to be converted to a string
* @return string representing object * @return string representing object
*/ */
@ -14,7 +14,7 @@ public class StringUtils {
// to replace the DateFormat with java.time.format.DateTimeFormatter // to replace the DateFormat with java.time.format.DateTimeFormatter
if (o instanceof OffsetDateTime) { if (o instanceof OffsetDateTime) {
OffsetDateTime odt = (OffsetDateTime)o; OffsetDateTime odt = (OffsetDateTime)o;
return odt.format(DateTimeFormatter.ISO_INSTANT); return odt.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
} else if (o == null) { } else if (o == null) {
return "null"; return "null";
} else { } else {

View File

@ -117,9 +117,12 @@ public class ToFromConversionTests extends RefineTest {
Assert.assertEquals(invoke("toString", Double.valueOf(100.0)),"100.0"); Assert.assertEquals(invoke("toString", Double.valueOf(100.0)),"100.0");
Assert.assertEquals(invoke("toString", Double.valueOf(100.0),"%.0f"),"100"); Assert.assertEquals(invoke("toString", Double.valueOf(100.0),"%.0f"),"100");
String expectedDate = "2013-06-01T00:00:00Z"; String intputDate = "2013-06-01";
Assert.assertEquals(invoke("toString", CalenderParser.parseAsOffsetDateTime("2013-06-01")), expectedDate); String expectedDate = "2013-06-01";
Assert.assertEquals(invoke("toString", CalenderParser.parseAsOffsetDateTime("2013-06-01"),"yyyy-MM-dd"),expectedDate); Assert.assertEquals(invoke("toString", CalenderParser.parseAsOffsetDateTime(intputDate)),
expectedDate);
Assert.assertEquals(invoke("toString", CalenderParser.parseAsOffsetDateTime(intputDate), "yyyy-MM-dd"),
expectedDate);
} }
@Test @Test