Using java.util.TimeZone to get the current time for a specific time zone

I am creating a ColdFusion application that should display the current time in the number of time zones and was wondering if anyone can tell me how to do this using java.util.TimeZone ?

What I have gathered from various 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 time zone. Any help would be greatly appreciated. Thank you

+4
source share
1 answer

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

The second option is to use the String.format() method using the format string and the result of getTime() .

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

+4
source

Source: https://habr.com/ru/post/1499986/


All Articles