Those are sell by dates, not expiration dates. Many of them are phony. Many dry, canned and bottled goods can last years if stored properly, but those sell by dates are generaly a few days or a few week from when they hit store shelves. Used to be they were only on dairy products, meat, and fresh vegetables, and some prepaed foods such as potato chips, not canned or bottled goods. Now they appear on everything. They started putting them on those other products just a few years ago because they discovered that many consumers are dumb enough to throw out food after the stamped date and buy more, thereby increasing sales of those products. America is the land of waste.
It's legal because they are not expired, there is no laws limiting their shelf life - as if shelf life could be determined by law. That would be like politicians deciding it can only rain on tuesdays.