Am trying convert date which is in local time zone to GMT, i did some thing like this
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
String gmtStrDate = sdf.format(Calendar.getInstance().getTime());
System.out.println("GMT 1"+gmtStrDate);
Date gmtDate = sdf.parse(gmtStrDate);
System.out.println("GMT 2"+gmtDate);
i am getting GMT 1 o/p in GMT tz but GMT 2 o/p is again changing to local time zone...Can any tel me why?
to get the GMT 2 o/p in GMT i did like this :
Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("GMT")); calendar.setTime(gmtDate);
System.out.println("GMT 3 = "+calendar.getTime());
but still GMT 3o/p am getting in local tz. Please suggest on this.
(1) In above formulas, 9 is the number of hours the local time ahead to GMT, and you can change it as you need, if the local time is backward to the GMT, you just can change the plus sign + to minus sign -. (2) The formula =A2 + (9 / 24) will return a decimal number.
You can make use of the following DateFormat. SimpleDateFormat myDate = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss"); myDate. setTimeZone(TimeZone. getTimeZone("UTC")); Date newDate = myDate.
The java. util. Date has no concept of time zone, and only represents the number of seconds passed since the Unix epoch time – 1970-01-01T00:00:00Z. But, if you print the Date object directly, the Date object will be always printed with the default system time zone.
GMT is a UTC +00:00 timezone offset where as India Standard Time (IST) is a UTC +5:30 timezone offset. Time difference between GMT and India Standard Time (IST) is 6:30 hours ie., India Standard Time (IST) time is always 6:30 hours ahead of GMT.
import java.util.*;
import java.text.*;
public class CurrentTimeToGMT{
public static void main(String args[]){
Date date = new Date();
DateFormat gmtFormat = new SimpleDateFormat();
TimeZone gmtTime = TimeZone.getTimeZone("GMT");
gmtFormat.setTimeZone(gmtTime);
System.out.println("Current Time: "+date);
System.out.println("GMT Time: " + gmtFormat.format(date));
}
}
Output
Current Time: Wed Mar 10 11:21:18 IST 2010 GMT Time: 3/10/10 5:51 AM
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With