Return to Snippet

Revision: 79
at June 29, 2006 21:28 by vijucat


Initial Code
/*
   * Converts time from sourceTZ TimeZone to destTZ TimeZone.
   * 
   * @return converted time, or the original time, in case the datetime could not be parsed 
   * 
   */
  private String convTimeZone(String time, String sourceTZ, String destTZ)
  {
    final String DATE_TIME_FORMAT = "yyyyMMdd-HH:mm:ss";

    SimpleDateFormat sdf = new SimpleDateFormat(DATE_TIME_FORMAT);

    Date specifiedTime;
    try {
      if (sourceTZ != null)
        sdf.setTimeZone(TimeZone.getTimeZone(sourceTZ));
      else
        sdf.setTimeZone(TimeZone.getDefault()); // default to server's timezone
      specifiedTime = sdf.parse(time);
    }
    catch (Exception e1) {
      try {
        specifiedTime = new Time(time).getAsDate();
      } catch (Exception e2) {
        // 
        return time;
      }
    }

    // switch timezone
    if (destTZ != null)
      sdf.setTimeZone(TimeZone.getTimeZone(destTZ));
    else
      sdf.setTimeZone(TimeZone.getDefault()); // default to server's timezone
    
    return sdf.format(specifiedTime);
  }

Initial URL


Initial Description
Converts a date/time from one timezone to another.

Initial Title
convert from one timezone to another

Initial Tags
date, java

Initial Language
Java