VisualVM中OQL查询结果格式化及ID值异常问题咨询
Let's break down your two issues and fix them step by step:
1. Formatting Long creationTime into a Human-Readable Date
The problem with sun.org.mozilla.javascript.internal.NativeDate@xxxxxx is that VisualVM's OQL uses the Rhino JavaScript engine, and NativeDate objects don't automatically render as readable strings in query results. Calling toString() might return Invalid Date if your creationTime uses the wrong unit (e.g., seconds instead of milliseconds) or holds an invalid value.
Fixes:
- Use
toLocaleString()for a ready-made readable format:select { id: s.getId(), createdAt: new Date(s.creationTime).toLocaleString() } from org.apache.catalina.session.StandardSession s - If
creationTimeis in seconds (not milliseconds): Multiply by 1000 to convert it to the correct timestamp format forDate:select { id: s.getId(), createdAt: new Date(s.creationTime * 1000).toLocaleString() } from org.apache.catalina.session.StandardSession s - Custom date formatting: If you need a specific format (like
YYYY-MM-DD HH:mm:ss), use a helper function in your OQL:select { id: s.getId(), createdAt: function() { var d = new Date(s.creationTime); // Pad single-digit values with leading zeros var pad = function(num) { return num < 10 ? "0" + num : num; }; return d.getFullYear() + "-" + pad(d.getMonth() + 1) + "-" + pad(d.getDate()) + " " + pad(d.getHours()) + ":" + pad(d.getMinutes()) + ":" + pad(d.getSeconds()); }() } from org.apache.catalina.session.StandardSession s
2. Why the Session ID Shows Incorrectly in VisualVM
The root cause is how you're accessing the session ID. In Tomcat's StandardSession, the internal id field is typically a byte array (not a String). Calling toString() on a byte array in Rhino returns the array's object identifier (like the numeric value you saw: 1010827848) instead of the hexadecimal session ID string.
Eclipse Memory Analyzer (MAT) automatically parses this byte array into the human-readable hex string you expect, but VisualVM doesn't do this by default.
Fix:
Instead of accessing the internal id field, call the public getId() method of StandardSession—it returns the properly formatted session ID string:
select { id: s.getId(), createdAt: new Date(s.creationTime).toLocaleString() } from org.apache.catalina.session.StandardSession s
This will give you the same hexadecimal ID (BE27C51E8BF185A2FB3AA9164EC0C647) that you see in MAT.
内容的提问来源于stack exchange,提问作者calvinkrishy

