After returning to his home village in Yunnan province on September 28 last year, Yang Qingpei murdered his parents when they refused to give him money, the Qujing City Intermediate People's Court said on its official microblog account.
Fearing he would be discovered, Yang then killed 17 of his neighbours and fled to Yunnan's provincial capital Kunming, it said, adding that the murder weapon was a pickaxe.
Yang, who was born in 1989, pleaded guilty to all charges and apologised to the relatives of the dead, it added.
While mass killings are rare, violent crime has been on the rise in China in recent decades as the economy has boomed and the gap between rich and poor has expanded rapidly.
Studies have also described a rise in the prevalence of mental disorders, some of them linked to stress as the pace of life becomes faster and support systems wither.