When the writ is returned, the court may hold the hearing immediately, unless the validity of a detention on any civil process, or the mental capacity of the party is to be determined, and may, in any case, set a date for the hearing, which shall be not more than five days after the return of the writ unless for good cause additional time is allowed.
Notice of the time and place set for a later hearing shall be served at least two days before the hearing or earlier, as the court may order, by the applicant upon the defendant, and (a) if the party is in custody on any criminal matter, upon the county prosecutor of the county in which the alleged offense was committed, or (b) if the party is in custody on any civil process, upon each person having an interest in continuing the confinement or restraint or upon the party's attorney, or (c) if the party is in custody of any psychiatric facility or other institution, upon the person or persons whose application was the basis for commitment to the facility or institution, and upon the medical director or other head officer of the facility or institution.
N.J.S. § 2A:67-27