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