Jason
Jason

Reputation: 1977

Coldfusion/Java - get current time in timezone different to the server

I've read just about every blog post on this topic that I can get my hands on, but just getting no joy.

I have a site running on a server in Perth, Australia (shared hosting, so server time is set to Perth time). But I need all activity to be recorded in Melbourne, Australia timezone. For example, if a record is written to the database, I want to record what time that occured in Melbourne, not perth (e.g. forum topic posted). I can't do a straight DateDiff as Melbourne has Daylight Savings, and Perth doesn't. Plus I figured while I am at this, I may as well come up with an approach that can give me the Current Melbourne (or anywhere) time regardless of the timezone of the server it is running on.

This task has turned into a much, much bigger challenge than I would have expected.

What I have come to is this:

    timezoneClass = createObject( "java", "java.util.TimeZone" );
    melbourneTimezone = timezoneClass.getTimeZone(javaCast( "string", "Australia/Melbourne" ));
    melbourneCalendar = createObject( "java", "java.util.GregorianCalendar" ).init(melbourneTimezone);
    melbourneTime = melbourneCalendar.getTime();

    writeDump(melbourneTime);

.. and from what I can tell, this should give me what I need, but it doesn't.. when I dump out 'melbourneTime', it just gives me the server time.

Can anyone give me any pointers. Am I on the right path, or is ther a much simpler way of acheiving the desired outcome.

Many Thanks

Jason

Upvotes: 4

Views: 1774

Answers (1)

Edy Ionescu
Edy Ionescu

Reputation: 1773

If you'll use rip747 / TimeZone-CFC, you can easily find your answer using the code below:

timezoneObj = createObject("component", "timezone").init();
melbourneTime = timezoneObj.castFromServer(now(), "Australia/Melbourne");

writeDump(melbourneTime);

Upvotes: 5

Related Questions