2

I am trying to test that a timestamp (a field in my entity) is older than a certain number of months.

My current code is:

if(person.getBirthday().before(Timestamp.valueOf(String.valueOf(LocalDateTime.now().minusMonths(12)){
   //do something
}

Note: birthday is a timestamp field and LocalDateTime is from: org.joda.time.LocalDateTime

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(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at com.intellij.rt.execution.CommandLineWrapper.main(CommandLineWrapper.java:130)
Caused by: java.lang.IllegalArgumentException: Timestamp format must be yyyy-mm-dd hh:mm:ss[.fffffffff]
    at java.sql.Timestamp.valueOf(Timestamp.java:202)

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

Paolo Forgia
  • 6,572
  • 8
  • 46
  • 58
java123999
  • 6,974
  • 36
  • 77
  • 121

1 Answers1

4

Here :

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

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 = LocalDateTime.now().minusMonths(12).toDateTime().getMillis()

EDIT :

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

if(person.getBirthday().before(new Timestamp(timestamp)) {
  [...]
}
Arnaud Denoyelle
  • 29,980
  • 16
  • 92
  • 148
  • Thanks, please also show how to compare using timestamp.before(), I don't think that the .before() method can take a long as a parameter? – java123999 Aug 17 '16 at 11:00
  • @java123999 No but you can use the long value to create a new `Timestamp`. Then use `TImestamp.before(Timestamp)`. – Arnaud Denoyelle Aug 17 '16 at 11:03
  • ok, how to create a new timestamp from the long value? I am only new to using timestamps and dates etc, hence the easy questions! – java123999 Aug 17 '16 at 11:05
  • @java123999 I edited the answer it order to show it. – Arnaud Denoyelle Aug 17 '16 at 11:06
  • Thanks for your help, what Time library would you suggest learning for use with Java? It seems timestamp is not very useful compared to others? – java123999 Aug 17 '16 at 11:08
  • @java123999 `java.sql.Timestamp` is quite old and was intended for JDBC compatibility. Depending on the java version you are using, I would advice `java.time.*` classes (from Java 8) or Joda-time if you use Java < 8. Actually, `java.time` is inspired from Joda-time (and is written by the same author) and some flaws of Joda-time have been patched in `java.time`. – Arnaud Denoyelle Aug 17 '16 at 11:13
  • Thanks, so try to use Java.time when possible? – java123999 Aug 17 '16 at 11:16