Picard (according to the TNG Writer/Director's Guide) was in command of the Stargazer for 22 years. Since we know he lost command in 2355 (with the loss of the Stargazer), that places his taking command in 2333.
DATA: Captain, he may refer to an incident which occurred nine years ago in the Maxia Zeta star system, in which an unidentified starship.
Picard assumed command of the Enterprise-D in 2364.
RALPH: What year is this?
DATA: By your calendar two thousand three hundred sixty four.
The prior intervening nine years have never been discussed in canon. The EU novel "The Buried Age" suggests that he was commanding another (unnamed) vessel for at least some of this time, during which he met Tasha Yar and Geordi La Forge.
Picard remained in command of the Enterprise-D until its destruction in 2371. He assumed command of the Enterprise-E in 2373 where he remained in command until his promotion to Admiral in 2381. It's reasonable to assume that he wasn't commanding a ship, nor would have have had command of a single vessel after that point, up until his retirement.
Picard's total time in command of his three canonically known vessels is (22 + 7 + 8 =) 37ish years. If you factor in possible temporary commands he could have held between the destruction of his primary vessels, this may be as much as 42 years.