1

I come across this line of code in my project:

String statusRecoTime = sdf.format(cal.getTime());

Then there is a null check like

if (statusRecoTime != null) {
     //do something
}

I think that the statusRecoTime will never be null and there is no need of this check because it is assigned with an object.

Please let me know if my understanding is correct?

Mistalis
  • 17,793
  • 13
  • 73
  • 97
Prakruti Pathik
  • 392
  • 4
  • 17
  • 5
    The answer depends on whether `sdf.format()` can ever return `null`. – Eran May 03 '17 at 07:49
  • 2
    What does the documentation of format say? :) – JacksOnF1re May 03 '17 at 07:49
  • 1
    What is your `sdf`, it's all depends on it – Yu Jiaao May 03 '17 at 07:49
  • You don't need a null check. You do need a try/catch – Tim May 03 '17 at 07:50
  • 4
    I guess `sdf` is `SimpleDateFormat` which should throw an `Exception` when an incorrect value is tried to be formatted. – jAC May 03 '17 at 07:50
  • Always try to check the condition whether the value is coming or not. Likewise always write the code with try catch block. code standard always should be follow when you write code – muthu vignesh May 03 '17 at 07:59
  • The only thing you gotta be careful about is the usage of `Calendar`. The month is 0-indexed, which means that `Calender.set(2017,01,03,23,59,59);` returns 03rd February 2017. That's quite annoying. – jAC May 03 '17 at 08:11
  • It’s an aside, @JanesAbouChleih, and it’s true (except it is definitely not the *only* thing), and the good solution is to use the `java.time` classes instead of the outdated `Calendar`. Changing your old code in this way could be a project of its own, though. – Ole V.V. May 03 '17 at 08:43

4 Answers4

2

From the SimpleDateFormat docs, the method returns a NullPointerException if the passed date is null:

SimpleDateFormat.format()

Formats the given Date into a date/time string and appends the result to the given StringBuffer.

Specified by:
format in class DateFormat
Parameters: date - the date-time value to be formatted into a date-time string.
toAppendTo - where the new date-time text is to be appended.
pos - the formatting position. On input: an alignment field, if desired. On output: the offsets of the alignment field.
Returns: the formatted date-time string.
Throws: NullPointerException - if the given date is null.

Checking statusRecoTime != null is not necessary, as you will have an exception if null.

Mistalis
  • 17,793
  • 13
  • 73
  • 97
0

No you do not need any null check here as if sdf will not be able to format the time it will throw error instead of null. although as you are formating time of calendar exception will also not come if you have defined correct format in sdf .

Satish Kumar
  • 110
  • 1
  • 11
0

Assuming that sdf is a SimpleDateFormat instance, format will never return null. The null check is completely unnecessary.

Various answers here concern themselves with whether that code will throw an exception. The only time it would is if sdf or cal were null. But assuming both are non-null and cal is a Calendar, that code won't throw an exception.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • but if the `format` is done within `class.method1`, `method1` is catching the possible exceptions accordingly and the second code snippet is executed within `class.method2`, wouldn´t this `null` check be necessary again? – SomeJavaGuy May 03 '17 at 07:56
  • @SomeJavaGuy: That's making a lot of assumptions of information not present in the question. Also note that provided `cal` and `sdf` are not `null`, and `cal` is a `Calendar`, and `sdf` is a `SimpleDateFormat`, the code in question won't throw. – T.J. Crowder May 03 '17 at 07:58
  • @T.J.Crowder : Thankyou – Prakruti Pathik May 03 '17 at 09:36
0

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.

Community
  • 1
  • 1
Ole V.V.
  • 81,772
  • 15
  • 137
  • 161