In the code below I have used calendar object to initialize time Zone to GMT and get the time accordingly but when I put back in date object, its automatically converting to my local time zone i.e. IST.
Calendar gmt = new GregorianCalendar(TimeZone.getTimeZone("GMT"));
Date dt=gmt.getTime();
Could anyone suggest a way through which I can retain the GMT format in date object also.
its automatically converting to my local time zone i.e. IST
No it's not. A Date
object doesn't have a time zone - it's just an instant in time.
You'll see the system-local time zone if you call toString()
on a Date
, because that's unfortunately what Date.toString()
does... but the time zone is not part of the information stored in a Date
.
If you want to see a textual representation of a Date
in a particular time zone, use DateFormat
and set the time zone that you want to use.
See more on this question at Stackoverflow