title: converting date from one time zone to other time zone in java
body: <p>I wanted to convert a date from one time zone to another, using SimpleDateFormat class in java. But somehow it is generating different results which are suppose to be in the same TimeZone. </p>
  
  <p>Here is a test case, and its generating one result as IST and other one as GMT. i think it should be generating only GMT's for both cases.</p>
  
  <pre><code>public class TestOneCoreJava {
  
      public static void main(String[] args) throws ParseException {// Asia/Calcutta
          DateFormat formatter = new SimpleDateFormat("dd-MMM-yy hh:mm:ss a");
          System.out.println(getDateStringToShow(formatter.parse("26-Nov-10 03:31:20 PM +0530"),"Asia/Calcutta", "Europe/Dublin", false));
          System.out.println(getDateStringToShow(formatter.parse("02-Oct-10 10:00:00 AM +0530"),"Asia/Calcutta", "Europe/Dublin", false));
          //------Output--
          //26-Nov-10 GMT
          //02-Oct-10 IST
      }
  
      public static String getDateStringToShow(Date date,
              String sourceTimeZoneId, String targetTimeZoneId, boolean includeTime) {
          String result = null;
  
          // System.out.println("CHANGING TIMEZONE:1 "+UnitedLexConstants.SIMPLE_FORMAT.format(date));
          String date1 = new SimpleDateFormat("dd-MMM-yy hh:mm:ss a").format(date);
  
          SimpleDateFormat sourceTimeZoneFormat = new SimpleDateFormat("Z");
          sourceTimeZoneFormat.setTimeZone(TimeZone.getTimeZone(sourceTimeZoneId));
  
          date1 += " " + sourceTimeZoneFormat.format(date);
  
          // Changed from 'Z' to 'z' to show IST etc, in place of +5:30 etc.
          SimpleDateFormat targetTimeZoneFormat = new SimpleDateFormat("dd-MMM-yy hh:mm:ss a z");
          targetTimeZoneFormat.setTimeZone(TimeZone.getTimeZone(targetTimeZoneId));
  
          SimpleDateFormat timeZoneDayFormat = null;
          if (includeTime) {
              timeZoneDayFormat = targetTimeZoneFormat;
          } else {
              timeZoneDayFormat = new SimpleDateFormat("dd-MMM-yy z");
          }
          timeZoneDayFormat.setTimeZone(TimeZone.getTimeZone(targetTimeZoneId));
          try {
              result = timeZoneDayFormat.format(targetTimeZoneFormat.parse(date1));
              // System.out.println("CHANGING TIMEZONE:3 "+result);
          } catch (ParseException e) {
              e.printStackTrace();
          }
          return result;
      }
  }
  </code></pre>