Let's consider having 2-D Cartesian coordinate xy-plane, a block connected to a spring (placed horizontally on negative part of x-axis and having natural length L) sitting at the origin point with the spring at natural length.It is allowed to oscillate back and forth about the origin. From the equation that relates the work done by conservative force and the potential energy, $U({x}_{a})-U({x}_{b})=-{\int}_{{x}_{b}}^{{x}_{a}}{F}_{x}\text{d}x$. If we define the zero point at x=0, we can express its potential energy for $0<x<L$, with

$U(x)-U(x=0)=-{\int}_{0}^{x}-kx\text{d}x\Rightarrow U(x)=\frac{1}{2}k{x}^{2}$

while for −L<x<0, we have (the restoring force switch sign as it now points to the positive x- direction):

$U(x)-U(x=0)=-{\int}_{0}^{x}kx\text{d}x\Rightarrow U(x)=-\frac{1}{2}k{x}^{2}$

My question: Is my idea and this approach for the energy of the system correct? Because I saw from some reference books, they simply say that it is just $\frac{1}{2}k{x}^{2}$ for both of the conditions.