I am getting data from database in string format which is either 'y', 'n' or null(empty). I need to convert this value to Boolean 'y' to true, 'n' to false and null to null.
string 'y' -> true (Boolean)
string 'n' -> false (Boolean)
string null -> null (Boolean)
I tried bunch of methods but seems like null is always converting to false.
I have tried following methods
"Y".equalsIgnoreCase(str)
BooleanUtils.toBooleanObject(str)
Any help would be appreciated. Thanks in Advance
Just make sure to use Boolean
(capital B).
String foo = "y";
Boolean b = foo == null ? null : "y".equals(foo);
This will set b
to null
(if foo
is null
), or to true
(if foo == "y"
) or false
in all other cases.
If you want it as a function:
private Boolean getBooleanFromDB( String value ) {
Boolean result = value == null ? null : "y".equals(value);
return result;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.