The Bible does not reveal how long before David became king that he fought the wars of the Lord against His enemies, but we do know that David was king for forty years. David may have been a late teenager when we first meet him in the Bible, fighting Goliath, because the minimum age for a warrior was twenty, and David was not involved in the Philistine war at the time.
If that be true, he led Israel for fifty years minimum, and the Bible shows how the enemies always fell before him,
Then the Syrians fled before Israel; and David killed seven hundred charioteers and forty thousand horsemen of the Syrians…And when all the kings who were servants to Hadadezer saw that they were defeated by Israel, they made peace with Israel and served them… (2 Samuel 10:15–19).
Now that is what happens when you remain loyal to the Lord, and do His work.