public static String toNullableString ( Object value ) { // Shortcuts if ( value == null ) return null ; if ( value instanceof String ) return ( String ) value ; // Legacy and new dates if ( value instanceof Date ) value = ZonedDateTime . ofInstant ( ( ( Date ) value ) . toInstant ( ) , ZoneId . systemDefault ( ) ) ; if ( value instanceof Calendar ) { value = ZonedDateTime . ofInstant ( ( ( Calendar ) value ) . toInstant ( ) , ( ( Calendar ) value ) . getTimeZone ( ) . toZoneId ( ) ) ; } if ( value instanceof Duration ) value = ( ( Duration ) value ) . toMillis ( ) ; if ( value instanceof Instant ) value = ZonedDateTime . ofInstant ( ( Instant ) value , ZoneId . systemDefault ( ) ) ; if ( value instanceof LocalDateTime ) value = ZonedDateTime . of ( ( LocalDateTime ) value , ZoneId . systemDefault ( ) ) ; if ( value instanceof LocalDate ) value = ZonedDateTime . of ( ( LocalDate ) value , LocalTime . of ( 0 , 0 ) , ZoneId . systemDefault ( ) ) ; if ( value instanceof ZonedDateTime ) return ( ( ZonedDateTime ) value ) . format ( DateTimeFormatter . ISO_OFFSET_DATE_TIME ) ; // Convert list if ( value instanceof List < ? > ) { StringBuilder builder = new StringBuilder ( ) ; for ( Object element : ( List < ? > ) value ) { if ( builder . length ( ) > 0 ) builder . append ( "," ) ; builder . append ( element ) ; } return builder . toString ( ) ; } // Convert array if ( value . getClass ( ) . isArray ( ) ) { StringBuilder builder = new StringBuilder ( ) ; int length = Array . getLength ( value ) ; for ( int index = 0 ; index < length ; index ++ ) { if ( builder . length ( ) > 0 ) builder . append ( "," ) ; builder . append ( Array . get ( value , index ) ) ; } return builder . toString ( ) ; } // Everything else return value . toString ( ) ; }
Converts value into string or returns null when value is null .