Graceful transitions between periodic motions for nonlinear and hybrid systems