I have the following lines at the top of an email template (testing out a strange behaviour I’m seeing):
#set($sdf = $spTools.getClass().forName("java.text.SimpleDateFormat").getInstance()) $sdf.getClass().getName()
#set($tz = $spTools.getClass().forName("java.util.TimeZone").getInstance()) $tz.getClass().getName()
#set($cal = $spTools.getClass().forName("java.util.Calendar").getInstance())$cal.getClass().getName()
But all that’s running is this (three times, identical ):
java.text.SimpleDateFormat
java.text.SimpleDateFormat
java.text.SimpleDateFormat
Why is this happening?
narayanag
(NarayanaG G)
April 25, 2025, 7:08am
2
getInstance()
in Java does not necessarily return an instance of the base class . It often returns a subclass.
narayanag
(NarayanaG G)
April 25, 2025, 7:14am
3
What your code does:
#set ($sdf = $spTools.getClass().forName(“java.text.SimpleDateFormat”).getInstance())
$sdf.getClass().getName()
This is trying to create an instance of SimpleDateFormat via Class.forName().getInstance()
Then you’re printing out the class name to see what it actually created.
#set ($tz = $spTools.getClass().forName(“java.util.TimeZone”).getInstance())
$tz.getClass().getName()
Same idea for TimeZone.
#set ($cal = $spTools.getClass().forName(“java.util.Calendar”).getInstance())
$cal.getClass().getName()
And again for Calendar.
getInstance() in Java does not necessarily return an instance of the base class. It often returns a subclass.
For example:
java.text.SimpleDateFormat.getInstance() → returns a pre-configured SimpleDateFormat object.
java.util.Calendar.getInstance() → returns an instance of GregorianCalendar.
java.util.TimeZone.getInstance() → returns the system’s default TimeZone, usually a subclass like sun.util.calendar.ZoneInfo (in Oracle JDK).
If you’re testing behavior:
$sdf.format($cal.time)
$tz.getID()
set specific formats/time zones to test behavior changes:
#set ($sdf = $spTools.getClass().forName(“java.text.SimpleDateFormat”).newInstance())
$sdf.applyPattern(“yyyy-MM-dd HH:mm:ss”)
$sdf.format($cal.time)
Why the behavior might be strange:
getInstance() in Java does not necessarily return an instance of the base class. It often returns a subclass.
For example:
java.text.SimpleDateFormat.getInstance() → returns a pre-configured SimpleDateFormat object.
java.util.Calendar.getInstance() → returns an instance of GregorianCalendar.
java.util.TimeZone.getInstance() → returns the system’s default TimeZone, usually a subclass like sun.util.calendar.ZoneInfo (in Oracle JDK).
That doesn’t seem to explain why java.text.SimpleDateFormat is returned for all three of them.
gmilunich
(Geoff Milunich)
June 10, 2025, 6:58pm
5
Where are you seeing that returned? In the UI for editing the EmailTemplate, or in the actual email that is sent?
gmilunich
(Geoff Milunich)
June 10, 2025, 7:02pm
6
I would consider changing it to split the calls. I have used this before and it works.
#set($calClass = $spTools.getClass().forName("java.util.Calendar"))
#set($TimeZoneClass = $spTools.getClass().forName("java.util.TimeZone"))
#set($NewCalInstance = $calClass.getInstance())
$NewCalInstance.setTime($certification.expiration)
...