I have of late, but wherefore I know not, lost all my mirth, forgone all custom of exercise; and indeed, it goes so heavily with my disposition that this goodly frame, the earth, seems to me a sterile promontory; this most excellent canopy, the air, look you, this brave o'erhanging firmament, this majestical roof fretted with golden fire: why, it appears no other thing to me than a foul and pestilent congregation of vapours.   --II.ii.301.

Like the great larva of Albert Dürer, Hamlet might be named 'Melancholia.'
He also has above his head the bat which flies emboweled, and at his feet science, the sphere, the compass, the hourglass, love, and behind him in the horizon an enormous sun which seems to make the sky but darker.
 - Victor Hugo, William Shakespeare, in Jonathan Bate, The Romantics on Shakespeare, Penguin,London, 1992. p.351

Melancholy was a particularly widespread affliction in Elizabethan England; particularly amongst the educated classes. It seems to have been reserved more for men than for women (who had more of a penchant for hysteria), and it brings with a sense of being trapped in a prison of gloom which is reflected in Hamlet's world where "Denmark is a prison." (II.ii.247)

Timothy Bright, the most important student of melancholy during the Elizabethan period provides us with a telling picture of the malady which is amply reflected in Hamlet.

...the fancy ouertaken with gastly fumes of melancholy, and the whole force of the spirit closed vp in the dungion of melancholy darkness, imagineth all dark, blacke and full of feare...
 - Timothie Bright. Treatise of Melancholie. (Repro of 1586) New York: Columbia Univ. Press. 1940. p.189.

Several modern writers on Elizabethan society have noted the prevalence of this malady. Here are a few examples from important authors who have dealt with this topic.

Melancholy was very much in vogue in the England of Elizabeth and the early Stuarts, especially among the intellectuals and would-be intellectual. Elizabethan and early Stuart literature, consequently, abounds in references to melancholy and in melancholy characters.
 - Lawrence Babb, The Elizabethan Malady, Michigan State College Press, 1951.p.vii.

The last years of Queen Elizabeth and the reign of James I, towards the end of which Burton's Anatomy appeared, were marked by the prevalence of a particular kind of melancholy. All classes were affected by it... It gave the tone to a group of tragedies produced in the early years of the century provided a background even for comedy.
 - L.C.Knights, Drama and Society in the Age of Jonson, Chatto and Windus, London.1937. p.31

Robert Burton published the first edition of the Anatomy of Melancholy in 1621, and the fame of his book has somewhat obscured the fact that the author was not the first but the latest of the great melancholics of the seventeenth century. Melancholy, indeed, is one of the commonest strains in English writers... in the generation preceding the Anatomy it was the prevailing mood with intelligent writers. Few of them escaped it.
 - G.B.Harrison, "On Elizabethan Melancholy" in Melancholike Humours (Nicholas Breton). Scholartis Press, London. 1929. p49.

A briefe of sorrowe
Muse of sadness, neere deaths fashion,
Too neere madnesse, write my passion.
Paines possesse mee, sorrows spill me,
Cares distress me, all would kill mee.
Hopes have faild me, Fortune foild mee,
Feares have quaild me, all have spoild mee.
Woes have worne mee, sighes have soakt mee,
Thoughts have torne mee, all have broke mee.
Beauty strooke me, love hath catcht mee,
Death hath tooke mee, all dispatcht mee.
- Nicholas Breton, Melancholike Humours (1600), Scholartis Press, London. 1929. p.14,

L.C. Knights associates the fascination with melancholy with two social trends that were particular to late sixteenth century and early seventeenth century England. These were the prevalence of death through plague and warfare and the failure of the society to provide occupations for its educated class. In particular, the lack of opportunities for the educated (generally men) provides one of the common faces of melancholy, and the afflicted were frequently scholars; much like Hamlet.

The realization of death was one of the most important factors in producing melancholy. Men of the Middle Ages had also been fascinated by death,...but they had not fallen victims to the kind of melancholy under discussion. ....On the Continent the sixteenth and seventeenth centuries were a period of almost continuous warfare, and both abroad and in England the plague continued to exact its enormous toll of human life.
 - L.C.Knights,Drama and Society in the Age of Jonson. Chatto and Windus. London. 1937.pp. 320-321.

Under Elizabeth there had been a considerable increase of educational activity, with a consequent heightening of men's expectations. Even before the close of the sixteenth century there were more than a few who could find no place in the existing organization of the state... There is no need to stress the hardships of university men and the difficulty that they had in obtaining suitable employment. Scholars and writers were not the only discontented members of the commonwealth. Those who sought a public career were just as likely to have their hopes thwarted, or if they achieved success, it was only after long years of disappointment and delay.
 - pp. 324-327.