Normalizing date value to a single timezone

Hi,

Am trying to get a normalized date value irrespective of the time zone of the machine in which following code is run.

When the following code is run in 2 different machines with TZ=UTC and TZ=PDT, I get 2 different values.

I simply want to normalize the output that is specific to a single timezone, therefore UTC. Be it any timezone, I want the output to be always in UTC. Is there a way to achieve that?

I was hoping,

output.setTimeZone(TimeZone.getTimeZone("UTC"));

but it didn't.

        String dateVal = "2007-02-23T20:14:33.123";
        String dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSS";
        int daysToAdd = 2;

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(dateFormat);
        simpleDateFormat.setLenient(false);
        Date date = simpleDateFormat.parse(dateVal);
        System.out.println("dateval:" + date.toString());
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        System.out.println("calendar:" + calendar.toString());
        calendar.add(Calendar.DATE, daysToAdd);

        SimpleDateFormat output = new SimpleDateFormat(dateFormat);
        output.setTimeZone(TimeZone.getTimeZone("UTC"));

        System.out.println("final-val:" + output.format(calendar.getTime()));

Any pointers?

Thanks

Calendare.setTimeZone()? Java Platform SE 7 JDK 19 Documentation - Home

I did read that and that is the reason I have used that to normalize to a single time zone value whatever be the time zone value in the host from which its being run.

Still couldn't figure out a strategy where timezone is normalized to a single value.

Are you saying Calendar.setTimezone() should not be used?

Thanks for the reply

TimeZone (Java Platform SE 7 )) Can set the default time zone.