1

For some reason I always have the hardest time with getting timestamps to display correctly but anyway here is my problem.

I am pulling events from the Calendar Provider API and some events such as the US Holidays calendar events are in UTC so the timestamp is not what it should be on the device (unless the device is in that timezone of course).

I have a timestamp of this 1374105600000 which is 07/18/2013 00:00:00 UTC so july 18th at midnight UTC. What I want is the timestamp of july 18th at midnight local device time.

This is what I do

Calendar cal = Calendar.getInstance();
TimeZone tz = cal.getTimeZone();
cal.setTimeInMillis(time);

long offset = tz.getRawOffset(); //gives me -18000000 5hr difference since I am in EST which I think is actually wrong because it should be a 4hr difference now with DST

So if I add this to the UTC timestamp

long local = time+offset;

it gives me the incorrect time july 17th at 3:00PM

If I subtract the times

long local = time-offset;

I still get the wrong time, it gives me july 18th at 1:00AM but I dont think I should even be subtracting because that wont work for people in + timezone differences.

What am I doing wrong, why can I not get the correct offset to get the correct time?

I was also using this like as reference too Link

Community
  • 1
  • 1
tyczj
  • 71,600
  • 54
  • 194
  • 296

2 Answers2

2

Since Java does not ATTACH the Timezone information with a Date object, its a little weird to do the conversion. Please check below listing, where I am trying to convert the time from "UTC" (GMT) to "EST" (can be New York's timezone)

import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

public class TimeZoneTest {

    /**
     * @param args
     */
    public static void main(String[] args) {
        Calendar gmtTime = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
        Calendar estTime = Calendar.getInstance(TimeZone.getTimeZone("America/New_York"));
        
        System.out.println(getInputDate() + " (Actually GMT)");
        estTime.setTime(getInputDate());
        
        gmtTime.clear();
        gmtTime.set(estTime.get(Calendar.YEAR), estTime.get(Calendar.MONTH), 
                estTime.get(Calendar.DAY_OF_MONTH), estTime.get(Calendar.HOUR_OF_DAY), estTime.get(Calendar.MINUTE));
        gmtTime.set(Calendar.SECOND, estTime.get(Calendar.SECOND));
        Date estDate = gmtTime.getTime();
        System.out.println(estDate + "(Actually EST)");
    }
    
    private static Date getInputDate() {
        Calendar instance = Calendar.getInstance();
        instance.clear();
        instance.set(2014, 3, 2, 9, 0, 0);
        Date input = instance.getTime();
        return input;
    }

}

And the output is

Wed Apr 02 09:00:00 IST 2014 (Actually GMT)
Wed Apr 02 05:00:00 IST 2014(Actually EST)

which is actually correct

EDIT: Its important to use "America/New_York" instead of "EST" to consider Day light Saving

Community
  • 1
  • 1
sanbhat
  • 17,522
  • 6
  • 48
  • 64
0

hmmm.. the code below reverses your scenario , but maybe u can make use of it??

private  Date cvtToGmt( Date date )
{
   TimeZone tz = TimeZone.getDefault();
   Date ret = new Date( date.getTime() - tz.getRawOffset() );

   // if we are now in DST, back off by the delta.  Note that we are checking the GMT date, this is the KEY.
   if ( tz.inDaylightTime( ret ))
   {
      Date dstDate = new Date( ret.getTime() - tz.getDSTSavings() );

      // check to make sure we have not crossed back into standard time
      // this happens when we are on the cusp of DST (7pm the day before the change for PDT)
      if ( tz.inDaylightTime( dstDate ))
      {
         ret = dstDate;
      }
   }

   return ret;
}
Robert Rowntree
  • 6,230
  • 2
  • 24
  • 43