2 Sam 5:4, 5 says this:
David was thirty years old when he became king, and he reigned forty
years. In Hebron he reigned over Judah seven years and six months, and
in Jerusalem he reigned thirty-three years over all Israel and Judah.
There is a big difference between being anointed for a job and actually doing the job as these verse make clear.
It is true that David was anointed king over all Israel at the age of 30. BUT he did not reign over all Israel until the the king of non-Judah, Ish-bosheth, had been removed. The sequence of events was as follows:
- Saul died (1 sam 31)
- Ish-bosheth was anointed king over "all Israel", 2 Sam 2:9
- Almost simultaneously, David is anointed king over Israel, 2 Sam 2, 4, 5:4, 5, but in reality, David only reigned over Judah
- A civil war ensured, 2 Sam 2:12 - 2 Sam 4:12
- Finally, Is-bosheth was murdered, 2 Sam 4:1-8
- This left David free to begin his reign over all Israel, 7½ years later at the age of 37, 2 Sam 5:4, 5.
- David moves his capitol from Hebron to Jerusalem after conquering Jerusalem, 2 Sam 5:6-11
In fact, the (arguablly) premature anointing of David as king over "all Israel" was merely a repetition and recognition of David's earlier anointing as the heir apparent by the prophet Samuel under God's direction as recorded in 1 Sam 16:1-13, and God's rejection of the king Saul and his subsequent dynasty in 1 Sam 15.