AlanJames
AlanJames

Reputation: 127

Using java.util.TimeZone to get current time for a particular timezone

I am creating a ColdFusion Application that needs to display the current time in in a number of timezones and was wondering if anyone could tell me how to do this using java.util.TimeZone ?

What I have currently is assembled from different posts/articles :

<cfset timezoneClass = createObject( "java", "java.util.TimeZone" ) />
<cfset pragueZoneId = "Europe/Prague" />
<cfset pragueTimezone = timezoneClass.getTimeZone(javaCast( "string", pragueZoneId )) />
<cfset pragueCalendar = createObject( "java", "java.util.GregorianCalendar" ).init(pragueTimezone) />

I just don't know how to apply the above to get the current time for the timezone. Any help would be greatly appreciated. Thanks.

Upvotes: 4

Views: 370

Answers (1)

Mario Rossi
Mario Rossi

Reputation: 7799

You need to create a Java SimpleDateFormat object and configure it with the desired format you want to display the date/time. Then you can extract the Date from the GregorianCalendar using its getTime() method and pass this value to SimpleDateFormat to produce a String that you can display.

A second variant is be to use method String.format() using a format string and the result of getTime().

Alternatively, you can use method toString() of the GregorianCalendar object, but this is not recommended.

Upvotes: 4

Related Questions