I am assuming sdf
is a SimpleDateFormat
instance.
First thing to check is the documentation, as Mistalis already did in an answer. The one-argument format
method is declared in the superclass DateFormat
, and the documentation says
Returns:
the formatted time string.
This should be enough. There is no mention that it could return null
, so there should be no need to check. We could stop here.
If you want to be even more sure, we can check the source code for the Java version you are using. In Java 8 (and I would expect, in all versions) format(Date)
calls a three-argument format
method, gets a StringBuffer
back and calls its toString()
method. StringBuffer.toString()
makes a new String()
. new
is guaranteed never to return null
. So now we can be sure.
Except: An evil person might write a subclass of SimpleDateFormat
in which format(Date)
may return null
in conflict with the documentation. Your sdf
variable could hold an instance of such an evil subclass. You could get null
. If you know from your code that sdf
is always a SimpleDateFormat
and not some homegrown or third-party subclass, we can rule out this possibility.