Last active
December 24, 2015 13:29
-
-
Save alan-morey/6805580 to your computer and use it in GitHub Desktop.
Unexpected result when using String.valueOf() to get the value of a Date and DateTime, when using an Object reference.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Asserts fail for the following | |
///////////////////////////////// | |
DateTime dt = DateTime.now(); // VARIABLE_ASSIGNMENT dt = 2013-10-03T05:35:37.373Z | |
System.assertEquals(String.valueOf(dt), String.valueOf((Object) dt)); | |
// Assertion Failed: Expected: 2013-10-02 22:35:37, Actual: 2013-10-03 05:35:37 | |
Date d = Date.today(); // VARIABLE_ASSIGNMENT d = 2013-10-02T00:00:00.000Z | |
System.assertEquals(String.valueOf(d), String.valueOf((Object) d)); | |
// Assertion Failed: Expected: 2013-10-02, Actual: 2013-10-02 00:00:00 | |
// All below assertions pass as expected | |
//////////////////////////////////////// | |
Blob b = Blob.valueOf('foobar'); | |
System.assertEquals(String.valueOf(b), String.valueOf((Object) b)); | |
Boolean bo = true; | |
System.assertEquals(String.valueOf(bo), String.valueOf((Object) bo)); | |
Double db = 1.2345; | |
System.assertEquals(String.valueOf(db), String.valueOf((Object) db)); | |
Decimal dc = 678.91011; | |
System.assertEquals(String.valueOf(dc), String.valueOf((Object) dc)); | |
Integer i = 42; | |
System.assertEquals(String.valueOf(i), String.valueOf((Object) i)); | |
Long l = 2147483648L; | |
System.assertEquals(String.valueOf(l), String.valueOf((Object) l)); | |
String s = 'foobar'; | |
System.assertEquals(String.valueOf(s), String.valueOf((Object) s)); | |
Id id = '001000000000000AAA'; | |
System.assertEquals(String.valueOf(id), String.valueOf((Object) id)); | |
Time t = DateTime.now().time(); | |
System.assertEquals(String.valueOf(t), String.valueOf((Object) t)); | |
Object o = null; | |
System.assertEquals(String.valueOf(o), String.valueOf(o)); | |
class FooBar { | |
Integer ii = 1; | |
String ss = 'baz'; | |
} | |
FooBar f = new FooBar(); | |
System.assertEquals(String.valueOf(f), String.valueOf((Object) f)); | |
class FooBarWithToString { | |
Integer ii = 1; | |
String ss = 'baz'; | |
public override String toString() { | |
return ii + '-' + ss; | |
} | |
} | |
FooBarWithToString ff = new FooBarWithToString(); | |
System.assertEquals(String.valueOf(ff), String.valueOf((Object) ff)); | |
System.assertEquals(ff.toString(), String.valueOf(ff)); | |
System.assertEquals(ff.toString(), String.valueOf((Object) ff)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Workaround wraps the String.valueOf() and casts back to Date/DateTime | |
// object if Object o is an instance of a Date/DateTime, otherwise the Object is | |
// simply passed straight through to String.valueOf() | |
public class Strings { | |
public static String valueOf(Object o) { | |
if (o instanceof Date) { | |
return String.valueOf((Date) o); | |
} | |
if (o instanceof DateTime) { | |
return String.valueOf((DateTime) o); | |
} | |
return String.valueOf(o); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@isTest | |
public StringsTest { | |
@isTest static void testValueOfWithDateTime() { | |
DateTime dt = DateTime.now(); | |
System.assertEquals(String.valueOf(dt), Strings.valueOf((Object) dt)); | |
} | |
@isTest static void testValueOfWithDate() { | |
Date d = Date.today(); | |
System.assertEquals(String.valueOf(d), Strings.valueOf((Object) d)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment