Anyone know much about using the java.util.Date fr...
# cfml-general
p
Anyone know much about using the java.util.Date from the java object? I'm trying to convert a date to epoch time. This dump below will show me today's date in epoch time.
Copy code
writeDump( createObject("java", "java.util.Date").init().getTime()  );
However, I want to be able to pass it a date to get the epoch time and my efforts are failing. I'm not sure where and in what format to pass it a date. Thanks.
b
@PK This is a function I use to get the current GMT epoch time for a client
Copy code
function dateToGMTEpoch( required date thisDate ) {
		// current GMT epoch date
		return dateDiff( 's', '1970-01-01 00:00:00', ( dateAdd( 's', ( getTimeZoneInfo( 'pst' ).utcTotalOffset + ( getTimeZoneInfo( 'pst' ).isDSTon ? getTimeZoneInfo( 'pst' ).DSTOffset : 0 ) ), thisDate ) ) );
	}
Note, their servers are in PST time zone, which is why I'm getting the
pst
timezone info
You'd need to adjust this for whatever timezone your dates are in
There's prolly other ways to do it, but this works for me and is pure CFML 🙂
p
Thanks Brad :)
j
This is another option 🙂
Copy code
function getEpoch( required date timestamp ) {
	return int( arguments.timestamp.getTime() / 1000 );
}
Also, passing in a timezone to
getTimeZoneInfo
only works on Lucee
👍🏻 1
b
@jc Your version is an epoch in your time zone though, right? not a GMT time?
I put in that ticket when I was working on this last year for my client
It's been sitting at "to fix" ever since 😕
Lucee's timezone stuff is much more useful than Adobe's
I'd rather see the little stuff like this make it into ACF 2023 than GraphQL, but no one asked me 😆
👍 2
p
@jc This gives me "The getTime method was not found." (I'm testing on CF11. Perhaps that is the issue.)
s
A
java.util.Date
has no TZ itself so it depends on the TZ of the JVM (or the host server if you haven't overridden it). We have all our servers, our JVMs, and our databases all configured to UTC and then we translate to TZ for input/display based on the user's selected TZ (since we're a global company).
👍 1
https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/Date.html "Although the Date class is intended to reflect coordinated universal time (UTC), it may not do so exactly, depending on the host environment of the Java Virtual Machine." (leap second) You're better off using Java Time these days (Java 8 & above) which has UTC-based types and both timezone and offset based types.
b
<cfscript>
/* Arbitrary date-time in the format MMM dd yyyy HH:mm:ss Z.
My timezone is Central European Summer Time (offset: UTC/GMT +2 hours)*/
dateFormat = createObject("java", "java.text.SimpleDateFormat").init("MMM dd yyyy HH:mm:ss Z");
dateAsString = "Jun 10 2023 11:13:30 +0200";
/* Convert to java.util.Date object */
dateObject = dateFormat.parse(dateAsString);
/* Returns the number of milliseconds */
writeoutput("Milliseconds since January 1, 1970, 00:00:00 GMT = " & dateObject.getTime() );
</cfscript>