Pergunta

I am trying to convert a GMT time into my current timezone (which is GMT -7, California to be exact), but it is off by 1 hour behind.

Here is an example of a String that I am retrieving the values from: 2014-05-07T02:02:04.316Z

So I am 1 hour short of the correct time (6:02 PM instead of 7:02 PM). Why is this?

private Calendar setTimeStamp(String s) {
    int year = Integer.parseInt(s.substring(0, 4));
    int month = Integer.parseInt(s.substring(5, 7)) - 1;
    int day = Integer.parseInt(s.substring(8, 10));
    int hour = Integer.parseInt(s.substring(11, 13));
    int minute = Integer.parseInt(s.substring(14, 16));

    Calendar gmtCal = Calendar.getInstance();
    gmtCal.set(year, month, day, hour, minute);

    long gmtTimeInMillis = gmtCal.getTimeInMillis();
    long currentTimeInMillis = gmtTimeInMillis + TimeZone.getDefault().getRawOffset();
    Calendar adjustedCal = Calendar.getInstance(TimeZone.getDefault());
    adjustedCal.setTimeInMillis(currentTimeInMillis);

    return adjustedCal;
}
Foi útil?

Solução

This one hour difference will likely be due to daylight savings. Clock changes for California.

However you can refer to the getDSTSavings() function in the TimeZone class to get the current DST offset for your time zone.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top