Link to home
Start Free TrialLog in
Avatar of ank9
ank9

asked on

GMT to EDT

In my Java code, I am getting Time in GMT

Date recDateObj = getGMTDate();

Can you please let me know how I can convert this time to one in EDT time zone.



SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
use dateformater,

convert your GTM date to date string in GMT, for e.g., 01 JAN 2010 01:01:01 GMT
and then use below formatter to parse it to Date back, before doing so, set the system default timezone to EST/EDT
SimpleDateFormat sdf = new SimpleDateFormat( "dd MMM yyyy HH:mm:ss z" ) ;
btw, prefer timezones using the city names to three-letter codes
Avatar of ank9
ank9

ASKER

Is this correct

Date recDateGMT = idfTimeRecieveDate.getDate();
String recDateStr = recDateGMT.toGMTString();
TimeZone edtTimeZone = TimeZone.getTimeZone("EDT");

DateFormat df = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
df.setTimeZone(edtTimeZone);
String edtDateStr = df.format(recDateGMT);
Yes, that looks good although i'm not sure that you need to convert recDateGMT to a String first
Avatar of ank9

ASKER

Thank you.
I executed the following code

 It printed the following logs
=================GMT Date===================Fri Mar 28 19:08:46 EDT 2008
 ==================EDT Date =========03/28/2008 23:08:46
The logs suggest that the method which was supposed to return GMT time is returning EDT time. If it is really returning EDT time then why does the time change when the timezone is set to EDT (that is in the second print statement)?

Date recDateGMT = getGMTTime();
System.out.println("=================GMT Date==================="+recDateGMT.toString()); 
TimeZone edtTimeZone = TimeZone.getTimeZone("EDT"); 
DateFormat df = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss"); 
df.setTimeZone(edtTimeZone); 
String edtDateStr = df.format(recDateGMT); 
logger.debug("==================EDT Date ========="+edtDateStr);

Open in new window

>>The logs suggest that the method which was supposed to return GMT time is returning EDT time.

That's because it's using a default formatter, which looks like it's set to EDT already
If you want it to print GMT without fail, you must use the same method, the difference being with a GMT TimeZone
Avatar of ank9

ASKER

Thank you.
But why does the time change when formatter is explicitly set to EDT?
Avatar of ank9

ASKER

>>If you want it to print GMT without fail, you must use the same method, the difference being with a GMT TimeZone
 
I just want to get the correct EDT Timezone.
Probably because the method getGMTTime() (which you don't show) returns a different Date
>>Probably because the method getGMTTime() (which you don't show) returns a different Date

Ignore that bizarre comment.

>>But why does the time change when formatter is explicitly set to EDT?

Why would it *not* do if you're using different time zones?
Avatar of ank9

ASKER

>>Probably because the method getGMTTime() (which you don't show) returns a different Date

But if it returns a time which is already EDT time (as suggested by first print statement) then why does setting the timezone explicitly to EDT changes the time.
>>But if it returns a time which is already EDT time  ...

(See above)
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Yes, EDT is not actually a TZ available in itself in my system. EST is the principal one
This question brings up some interesting issues IMO, but for the time being, the below shows some of the things i mentioned above:
import java.util.*;
import java.text.*;

public class GmtEdt {
    public static void main(String[] args) {
	Date d = java.sql.Timestamp.valueOf("2008-03-28 23:08:46");
	TimeZone edt = TimeZone.getTimeZone("America/New_York");
	Calendar cal = Calendar.getInstance();
	cal.setTimeZone(edt);
	cal.setTime(d);
	TimeZone gmt = TimeZone.getTimeZone("UTC");
	DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z");
	df.setTimeZone(gmt);
	System.out.println(df.format(d));
	df.setTimeZone(edt);
	System.out.println(df.format(d));
	System.out.printf("Was date %s actually in dst? %b\n", df.format(d), (cal.get(Calendar.DST_OFFSET) != 0));
    }
}

Open in new window

:-)