If X(t) is a fundamental matrix of the linear nonautonomous Hamiltonian system dx/dt=JA(t)x(*) with X(0) T JX(0)=J, then X(t) T JX(t)=J for every t R. This symplectic property is called the weak invariance of the system (*). In this paper we first set up some numerical schemes preserving the weak invariance of the system (*) for numerical computation of the fundamental matrix X(t) with X(0) T JX(0)=J. Based on the above schemes, we give an algorithm of numerically periodic and numerically quasiperiodic solutions for the nonautonomous Hamiltonian system by using the exponential dichotomy of linear differential systems.