How to convert LocalDateTime to human readable string in OQL

105 Views Asked by At

I am trying to create an OQL query in Eclipse MAT to select a bunch of objects from a Java heapdump with LocalDateTime fields. Simply calling the .toString() function of the object just returns the object address, e.g. this:

SELECT mci.firstName.toString(), mci.importantDateTime.toString() AS mydatetime FROM com.example.MyClass$Inner mci WHERE (mci.firstName.toString() = "John")

returns John | java.time.LocalDateTime @ 0x83be8420

How do I convert the object to a human-readable datetime string?

Once again to make it crystal clear: I don't want to parse a date time in Java. I need an Object Query Language (OQL) statement that produces a human readable string out of a LocalDateTime field.

1

There are 1 best solutions below

0
On

Something like this could help:

SELECT ((((((((((((toString(l.date.year) + "-") + l.date.month) + "-") + l.date.day) + "T") + l.time.hour) + ":") + l.time.minute) + ":") + l.time.second) + ".") + l.time.nano) AS "Local date time" FROM java.time.LocalDateTime l 

Modify this replacing l with mci.importantDateTime.

Alternatively, you could write an extension for MAT with a class specific name resolver org_eclipse_mat_api_nameResolver for java.time.LocalDateTime, java.time.LocalDate and java.time.LocalTime.