When did David Hamelech write Tehillim? Or to be more technical, when did David write his first Tehillim and when did he write his last? Also was there a certain period of time within which he wrote a lot of the Tehillim?
To answer one part of your question:
King David wrote his last psalm (Psalm 72) close to his death, when he appointed King Shlomo as his successor. See Metzudot David and Rashi on 72:20.