We help IT Professionals succeed at work.

GMT to EDT

ank9
ank9 asked
on
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.



Comment
Watch Question

Top Expert 2016
Commented:
You convert when it comes to the time you want it as a String.

Create a TimeZone of EDT and format the date with SimpleDateFormat, setting the created TimeZone on the DateFormat first
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" ) ;
Top Expert 2016

Commented:
btw, prefer timezones using the city names to three-letter codes

Author

Commented:
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);
Top Expert 2016

Commented:
Yes, that looks good although i'm not sure that you need to convert recDateGMT to a String first

Author

Commented:
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

Top Expert 2016

Commented:
>>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
Top Expert 2016

Commented:
If you want it to print GMT without fail, you must use the same method, the difference being with a GMT TimeZone

Author

Commented:
Thank you.
But why does the time change when formatter is explicitly set to EDT?

Author

Commented:
>>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.
Top Expert 2016

Commented:
Probably because the method getGMTTime() (which you don't show) returns a different Date
Top Expert 2016

Commented:
>>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?

Author

Commented:
>>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.
Top Expert 2016

Commented:
>>But if it returns a time which is already EDT time  ...

(See above)
I think, TimeZone edtTimeZone = TimeZone.getTimeZone("EDT");
is taken as GMT, try TimeZone edtTimeZone = TimeZone.getTimeZone("EST");
Top Expert 2016

Commented:
Yes, EDT is not actually a TZ available in itself in my system. EST is the principal one
Top Expert 2016

Commented:
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

Top Expert 2016

Commented:
:-)
Mick BarryJava Developer
Top Expert 2010

Commented:
see the following to determine the available timezones

http://helpdesk.objects.com.au/java/can-i-find-all-available-timezones-for-a-country