Jithish P N
Jithish P N

Reputation: 2160

Local time to EST time Conversion

Android Local time to EST time Conversion

Code:

SimpleDateFormat serverDateFormat=new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
serverDateFormat.setTimeZone(TimeZone.getTimeZone("EST"));
Calendar calender= Calendar.getInstance(Locale.getDefault());
String  time=serverDateFormat.format(calender.getTime());

but i getting wrong time. one hour difference from right time. for eg : local time : Tue Jul 07 17:30:00 GMT+05:30 2015 formated time : 2015/07/07 07:00:00 right time : 2015/07/07 08:00:00

Upvotes: 0

Views: 3129

Answers (2)

Meno Hochschild
Meno Hochschild

Reputation: 44061

Your problem is using the identifier "EST" which stands for "Eastern Standard Time". As the name suggests it does not use daylight saving rules. Proof:

TimeZone tz = TimeZone.getTimeZone("EST");
long offset = tz.getOffset(System.currentTimeMillis()) / (1000 * 3600);
System.out.println(tz.getID() + ":" + tz.useDaylightTime() + "/" + offset);
// output: EST:false/-5

Use the timezone id "America/New_York" instead:

tz = TimeZone.getTimeZone("America/New_York");
offset = tz.getOffset(System.currentTimeMillis()) / (1000 * 3600);
System.out.println(tz.getID() + ":" + tz.useDaylightTime() + "/" + offset);
// output: America/New_York:true/-4

Then you will observe daylight saving time in July making an offset difference of (+05:30) - (-04:00) = +09:30 resulting in the expected local time 8 AM.

Upvotes: 2

Gaurav Vachhani
Gaurav Vachhani

Reputation: 349

hey please try this function for time conversion -

    public static String getTime(String time, SimpleDateFormat sdf) {
    String convertedTime = "";

    try {
        TimeZone timeZone = TimeZone.getDefault();
        Date postdate = sdf.parse(time);
        long postTimeStamp = postdate.getTime() + timeZone.getRawOffset();

        String dateString = sdf.format(new Date(postTimeStamp));

        convertedTime = dateString;


        // convertedTime = getLastTime(context, time);
    } catch (Exception e) {
        e.printStackTrace();
    }

    return convertedTime;
}

Upvotes: -1

Related Questions