0

I am trying to convert the date/Time of my machine into GMT time zone by using the following code:

DateFormat gmtFormat = new SimpleDateFormat("E MMM dd HH:mm:ss 'GMT' yyyy");
TimeZone gmtTime = TimeZone.getTimeZone("GMT");
gmtFormat.setTimeZone(gmtTime);
System.out.println("Orginal Date : " + new Date());
String s= gmtFormat.format(new Date());
System.out.println("Converted Date As String: "+ s);
System.out.println("Converted Date As date object : " + gmtFormat.parse(s));

Above code output :

Orginal Date : Mon Apr 21 21:04:06 AST 2014
Converted Date As String: Mon Apr 21 18:04:06 GMT 2014
Converted Date As date object : Mon Apr 21 21:04:06 AST 2014

However my problem when I parse the " Converted Date As String " to " Converted Date As date object " by using the parse function, but as you can notice that the date changes to the original date

Why is that happen ?


The problem is solve, when I do the following :

DateFormat gmtFormat = new SimpleDateFormat("E MMM dd HH:mm:ss 'GMT' yyyy");
TimeZone gmtTime = TimeZone.getTimeZone("GMT");
gmtFormat.setTimeZone(gmtTime);
System.out.println("Orginal Date : " + new Date());
String s= gmtFormat.format(new Date());
System.out.println("Converted Date As String: "+ s);
// Just I added Local Time Zone Formatand I use it to call parse instead of gmtFormat
DateFormat LocalFormat = new SimpleDateFormat("E MMM dd HH:mm:ss 'GMT' yyyy");
System.out.println("Converted Date As date object : " + LocalFormat.parse(s));

I am wondering why thats happen >> How the Local format related to parse Function ? Does any one know why?

Neno M.
  • 123
  • 1
  • 6

1 Answers1

1

The return value from DateFormat.parse(String) is a java.util.Date, which does not retain any information about time zone or locale. A Date is essentially a wrapper object around a long value representing a number of milliseconds since January 1, 1970 at 00:00 GMT. When its toString() method is invoked, the default implementation renders the date in your local system's default timezone.

If you want to create a date/time representation which stores both a timestamp and an associated time zone / locale, you should create a java.util.Calendar object (using Calendar.getInstance(...) and provide it with a TimeZone and/or a Locale, then set the time associated with the calendar with the Date object representing the date/time you want.

sumitsu
  • 1,481
  • 1
  • 16
  • 33
  • I tried the following code with calender but I get my local machine time even though I set the timeZone to GMT : SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Calendar cal =Calendar.getInstance(TimeZone.getTimeZone("GMT+00:00")); System.out.println(f.format(cal.getTime())); System.out.println(f.parse(f.format(cal.getTime()))); – Neno M. Apr 21 '14 at 19:12
  • You'll also need to keep this line from your original code, where you set the time zone on the formatter: `gmtFormat.setTimeZone(gmtTime);`. Keep in mind that whenever you call `Calendar.getTime()` or `DateFormat.parse()`, the return value is still a `Date`, so even if the object which produced it is time-zone-aware, the `Date` itself is just a long value (which will need to be given a time zone). – sumitsu Apr 21 '14 at 19:52
  • Sorry for annoying you, but what do you mean by "the Date itself is just a long value (which will need to be given a time zone)" – Neno M. Apr 21 '14 at 20:45