The Hannay angle of classical mechanics is generalized so that it is invariant under gauge transformations, which are a restricted class of canonical transformations. A distinction between the Hamiltonian and the energy is essential to make in time-dependent problems. A time-dependent generalized harmonic oscillator with a cross term in the Hamiltonian is taken as an example. The Hamiltonian of this system is not in general the energy. The energy, the time derivative of which is the power, is obtained from the equation of motion and related to the action variable. Hamilton’s equations give the time rate of change of the angle and action variables. The generalized Hannay angle is shown to be zero, and remains invariant under gauge transformations. On the other hand, if the original Hamiltonian is chosen as the energy, a nonzero generalized Hannay angle is obtained, but the power is given incorrectly. Nevertheless in the adiabatic limit, the total angle, which is the sum of the dynamical and Hannay angles, is equal to the one calculated from the correct energy.