David Cassidy's ass just got spanked by the legal system after pleading no contest to DUI -- the judge ordered him to enroll in a 90-day live-in rehab facility for alcohol abuse.
TMZ broke the story ... Cassidy was busted for DUI in L.A. back in January -- his third DUI arrest since 2010.
In addition to rehab, Cassidy was also sentenced to 60 months probation. He must also complete a 9-month alcohol program.
We were previously told Cassidy had enrolled in rehab last month. It's unclear if the judge will give him credit for his previous stint.
Cassidy had originally faced up to a year in jail -- so he should consider this a victory. Even if his wife still wants to divorce him.+
5:07 PM PT -- Cassidy's rep tells TMZ, the singer had already been enrolled in an inpatient rehab facility at the time of his sentencing today and the judge will be giving him credit for the 102 days he's served. We're told Cassidy will be transitioning into a monitored outpatient program (with a sober coach) early next month. The rep says, "David is very committed to his sobriety."