java.Util.Date object with date of different timezone
<p>I am trying to create a date object with time representing a date-time in a different timezone. Is it possible? I tried this. Can someone please explain what is wrong here.</p>

<pre><code>public static void test3() {

    Calendar calendar = Calendar.getInstance();
    calendar.setTimeZone(TimeZone.getTimeZone("America/New_York"));

    Date d1 = calendar.getTime();
    System.out.println("d1= " + d1);

    SimpleDateFormat sdf = new SimpleDateFormat("MMM dd, yyyy HH:mm:ss a z");
    sdf.setTimeZone(TimeZone.getTimeZone("America/New_York"));

    String dateInString = sdf.format(calendar.getTime());
    System.out.println("dateInString= " + dateInString);

    try {
        Date d2 = sdf.parse(dateInString);
        System.out.println("d2= " + d2);
    }
    catch (ParseException e) {
        e.printStackTrace();
    }
}
</code></pre>

<p>output:</p>

<pre><code>d1= Thu Jan 21 17:26:20 IST 2016
dateInString= Jan 21, 2016 06:56:20 AM EST
d2= Thu Jan 21 17:26:20 IST 2016
</code></pre>

<p><strong>UPDATE:</strong></p>

<p>If I dont care about the timezone and just need the time, I have a Date object with shows the time with wrong timezone. Used a new instance of SDF</p>

<pre><code>public static void test3() {

    Calendar calendar = Calendar.getInstance();
    calendar.setTimeZone(TimeZone.getTimeZone("America/New_York"));

    Date d1 = calendar.getTime();
    System.out.println("d1= " + d1);

    SimpleDateFormat sdf = new SimpleDateFormat("MMM dd, yyyy HH:mm:ss a");
    SimpleDateFormat sdf2 = new SimpleDateFormat("MMM dd, yyyy HH:mm:ss a");
    sdf.setTimeZone(TimeZone.getTimeZone("America/New_York"));

    String dateInString = sdf.format(calendar.getTime());
    System.out.println("dateInString= " + dateInString);

    try {
        Date d2 = sdf2.parse(dateInString);
        System.out.println("d2= " + d2);
    }
    catch (ParseException e) {
        e.printStackTrace();
    }
}
</code></pre>

<p>Output:</p>

<pre><code>d1= Thu Jan 21 18:36:51 IST 2016
dateInString= Jan 21, 2016 08:06:51 AM
d2= Thu Jan 21 08:06:51 IST 2016

enter code here
</code></pre>