
408 So.2d 800 (1982)
Curtis Lee HARRISON, Petitioner,
v.
Louie L. WAINWRIGHT, Secretary, Department of Corrections, Respondent.
No. 82-61.
District Court of Appeal of Florida, Fifth District.
January 15, 1982.
*801 James B. Gibson, Public Defender, and Michael S. Becker, Asst. Public Defender, Daytona Beach, for petitioner.
Jim Smith, Atty. Gen., Tallahassee, and Mark C. Menser, Asst. Atty. Gen., Daytona Beach, for respondent.

WRIT OF HABEAS CORPUS
DAUKSCH, Chief Justice.
Upon consideration of the Petition for Writ of Habeas Corpus and the response, it is the determination of this court that the petitioner is being held by the respondent illegally.
The petitioner was sentenced to serve a term in the state prison after his probation was revoked. The trial judge did not give the petitioner credit for time he had already served in jail as a condition of the probation which was revoked. This is error. Section 921.161(1), Florida Statutes, requires:
... the court imposing a sentence shall allow a defendant credit for all of the time he spent in the county jail before sentence. The credit must be for a specified period of time and shall be provided for in the sentence.

(emphasis supplied).
This case is like the case of Andrews v. State, 357 So.2d 489 (Fla. 1st DCA 1978), in that the trial judge first sentenced petitioner to one sentence and then when the error of that sentence was brought to his attention, by this court in this instance, he then resentenced the petitioner to a greater term and then gave him credit for the time served. It must be done correctly the first time. See also Meintzer v. State, 399 So.2d 133 (Fla.5th DCA 1981).
Because petitioner has served time in excess of his legal sentence he is ordered immediately discharged from custody. Either party may file a timely petition for rehearing but the respondent shall immediately release the petitioner.
WRIT GRANTED; PETITIONER RELEASED FROM CUSTODY.
SHARP and COWART, JJ., concur.
