java123999 java123999 - 1 year ago 107
Java Question

How to check that a timestamp is older than a certain number of months?

I am trying to test that a

(a field in my entity) is older than a certain number of months.

My current code is:

//do something

Note: birthday is a
field and
is from:

The issue is that this code is giving me the error:

Exception in thread "main" java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(
at sun.reflect.DelegatingMethodAccessorImpl.invoke(
at java.lang.reflect.Method.invoke(
at com.intellij.rt.execution.CommandLineWrapper.main(
Caused by: java.lang.IllegalArgumentException: Timestamp format must be yyyy-mm-dd hh:mm:ss[.fffffffff]
at java.sql.Timestamp.valueOf(

How else can I check that a timestamp is older than a certain number of months?

Answer Source

Here :

Timestamp.valueOf(String.valueOf( [...])

You are converting a LocalDateTime (from joda time) to a String and expect Timestamp.valueOf (from java.sql.Timestamp) to be able to parse it but they don't use the same date format.

You should convert the LocalDateTime to a timestamp (i.e. number of milliseconds) then compare from this point.

Example :

long timestamp =


Then you can create a new Timestamp from this long value and compare using Timestamp.before(Timestamp) :

if(person.getBirthday().before(new Timestamp(timestamp)) {
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download