Olympic swimmer Michael Phelps walks into the courthouse in Baltimore for his trial on DUI and other charges Friday. (Patrick Semansky/Associated Press)

Olympic swimming champion Michael Phelps pleaded guilty Friday morning to driving under the influence of alcohol, his second such offense in 10 years. But the 18-time Olympic gold medalist avoided jail time, allowing him to maintain a training regimen that appears to have him pointed toward the 2016 Summer Olympics in Rio de Janeiro.

Baltimore City District Court Judge Nathan Braverman gave Phelps, 29, a one-year suspended sentence and 18 months of supervised probation, nearly three months after he was charged with DUI when police pulled him over outside Baltimore’s Fort McHenry Tunnel for driving 84 mph in a 45-mph zone and crossing double yellow lines.

Phelps failed a field sobriety test, and his blood-alcohol level was later measured at 0.14, well above Maryland’s legal limit of 0.08.

“The last three months of my life have been some of hardest times I’ve ever gone through and some of the biggest learning experiences that I’ve ever had,” Phelps told reporters outside the courthouse. “I’m finding out a lot about myself. For this day, I’m happy to be moving forward and I’ll continue to grow from this and continue on my path of recovery.”

Braverman said he was optimistic Phelps had learned his lesson but warned that if he didn’t, “the only thing the court has left in its toolbox is jail.”

The terms of Phelps’s probation dictate that he abstain from alcohol use during those 18 months. Days after his latest arrest, Phelps entered a 45-day alcohol rehabilitation program in Arizona, and he has continued undergoing after-care and attending Alcoholics Anonymous meetings in Maryland.

“I think this has driven home, in a very meaningful way to him, what needs to be done,” Phelps’s attorney, Steve Allen, told reporters following the hearing.” I have the greatest degree of confidence this will not happen again.”

Phelps’s previous DUI charge, for which he also pleaded guilty and received probation before judgment, came in 2004, when he was 19. He also drew scrutiny in 2009 after a photo was published that appeared to show him inhaling marijuana from a water pipe.

Phelps, whose 22 overall medals make him the most decorated Olympian ever, retired from competition after the 2012 London Games but came out of retirement this spring.

He has been training in Baltimore and could swim in meets as soon as April, when a six-month suspension from USA Swimming in the wake of his latest arrest ends.

Although Phelps has not said whether he intends to swim in the 2016 Olympics, it is widely assumed within the sport that he will. His coach, Bob Bowman, did not return a telephone message Friday.

Among Phelps’s supporters in the courtroom Friday were his mother, Debbie, and former Baltimore Ravens linebacker Ray Lewis, a close friend. Phelps and Lewis shared a hug outside the courthouse after the hearing.

“The next couple years are going to be very challenging,” Phelps told reporters. “I’m very pleased and happy that I have the great support around me.”