In a project I'm involved I had to convert some dates in the time zone
UTC (GMT) time zone
- CST (Central Time) which is used by most states in Mexico . As the project is entirely based on Java technology, I found the task of finding ways to do this with libraries for Java.
- found that there are some bookstores, such as Quartz, implement a class for time zone conversion. But in my case, need not rely so heavily on third-party libraries or outside the Java API.
- found that with TimeZone and DateFormat classes I can do the conversion using the following sentences. public void cambiarZonaHoraria (Date date, String to) {DateFormat dateFormat = new SimpleDateFormat (); TimeZone.getTimeZone TimeZone tz = (to) dateFormat
System.out. println (dateFormat.format (date));} What this does is basically
instantiate SimpleDateFormat with the local date format create a TimeZone instance with the short name time zone specified by the parameter "to"
is assigned the new time zone object dateFormat using the method format () dateFormat object is the conversion of our date to a string representing the new date in the time zone you've created, and this date is printed on the screen.
In the same way you can make conversions between different zones and time zones.
Now, note that to create a TimeZone using the short name for the zone. This left a list
zones with their names and IDs that I got running this little program I found in Java Developers Almanac
:
- import java.util.Date; Import java.util.TimeZone;
- public class
- ListadoZonasHorarias {public static void main (String [] args) {
- Date date = new Date (); / / Get the ID of all time zones
String longname = tz.getDisplayName (tz.inDaylightTime (date), TimeZone.LONG) System . out.print (shortName + "\\ t \\ t Hope you can serve like me. Greetings.
0 comments:
Post a Comment