Energy spectrum of a confined two-dimensional particle in an external magnetic field