The problem of optimal control of a harmonic oscillator is considered. The energy of oscillator at the final time instant is used as the optimization criterion. The optimal control and optimal value of energy as function of this time instant is determined. The result obtained was compared with the solution of the problem of time-optimal operation at bringing the oscillator in the state of rest.