Boa tarde!
Pessoal, sempre tive certas duvidas relacionadas ao funcionamento de timezones e horário de verão no java.
Sei que o java descarta as configurações de horário de verão do sistema e utiliza as suas próprias, sendo possível atualizá-la através de uma ferramenta que a sun disponibiliza.
Devido ao horário de verão estou tendo diversos problemas em alguns clientes, e estou tentando entender a fundo como funcionam exatamente as datas com java.
Olhem no exemplo abaixo:
O código:
System.out.println("--------------------TIMEZONE SISTEMA--------------");
System.out.println(TimeZone.getDefault().toString());
TimeZone.setDefault(TimeZone.getTimeZone("Ect/GMT"));
System.out.println("--------------------TIMEZONE GMT 0----------------");
System.out.println(TimeZone.getDefault().toString());
Timestamp ts = new Timestamp(0);
try {
PreparedStatement stm = ConnectionFactory.getConnection().prepareStatement("SELECT CURRENT_TIMESTAMP TS \n");
ResultSet rs = stm.executeQuery();
while (rs.next()){
ts = rs.getTimestamp("TS");
}
System.out.println("Timestamp do banco: " + ts.toGMTString());
System.out.println("Timezone OFFSET do Banco: " + ts.getTimezoneOffset());
} catch (SQLException e) {
e.printStackTrace();
}
System.out.println("--------------------TIMEZONE GMT -3 (SP)---------");
TimeZone.setDefault(TimeZone.getTimeZone("America/Sao_Paulo"));
System.out.println(TimeZone.getDefault().toString());
// System.out.println("DAYLIGHT SAVINGS: " + TimeZone.getDefault().getDSTSavings());
System.out.println("TIMEZONE OFFSET: " + new Timestamp(0).getTimezoneOffset());
System.out.println("DATA ATUAL: " + new Date().toLocaleString());
Retorna as seguintes informações:
--------------------TIMEZONE SISTEMA--------------
sun.util.calendar.ZoneInfo[id=“GMT”,offset=0,dstSavings=0,useDaylight=false,transitions=0,lastRule=null]
--------------------TIMEZONE GMT 0----------------
sun.util.calendar.ZoneInfo[id=“GMT”,offset=0,dstSavings=0,useDaylight=false,transitions=0,lastRule=null]
Conectando a Banco de Dados…
Timestamp do banco: 12 Nov 2010 14:46:31 GMT
Timezone OFFSET do Banco: 0
--------------------TIMEZONE GMT -3 (SP)---------
sun.util.calendar.ZoneInfo[id=“America/Sao_Paulo”,offset=-10800000,dstSavings=3600000,useDaylight=true,transitions=129,lastRule=java.util.SimpleTimeZone[id=America/Sao_Paulo,offset=-10800000,dstSavings=3600000,useDaylight=true,startYear=0,startMode=3,startMonth=9,startDay=15,startDayOfWeek=1,startTime=0,startTimeMode=0,endMode=3,endMonth=1,endDay=15,endDayOfWeek=1,endTime=0,endTimeMode=0]]
TIMEZONE OFFSET: 180
DATA ATUAL: 12/11/2010 14:46:31
O banco retorna a mesma data que o new Date(), mesmo eu utilizando timezones diferentes.
Caso eu utilize o mesmo TimeZone, o resultado fica errado…
--------------------TIMEZONE SISTEMA--------------
sun.util.calendar.ZoneInfo[id=“GMT”,offset=0,dstSavings=0,useDaylight=false,transitions=0,lastRule=null]
--------------------TIMEZONE GMT 0----------------
sun.util.calendar.ZoneInfo[id=“GMT”,offset=0,dstSavings=0,useDaylight=false,transitions=0,lastRule=null]
Conectando a Banco de Dados…
Timestamp do banco: 12 Nov 2010 14:54:46 GMT
Timezone OFFSET do Banco: 0
--------------------TIMEZONE GMT -3 (SP)---------
sun.util.calendar.ZoneInfo[id=“GMT”,offset=0,dstSavings=0,useDaylight=false,transitions=0,lastRule=null]
TIMEZONE OFFSET: 0
DATA ATUAL: 12/11/2010 16:54:46
O Horário correto seria 14:54:46.
Caso eu deixe o timezone como America/Sao_Paulo, são acrescentadas duas horas na hora retornada do banco de dados…
Alguém sabe se isso seria alguma configuração especial? Alguma configuração no banco de dados?
Estou utilizando o SQL Server 2008 com windows 7.
JDK e JRE 1.6.0 u22
Agradeço qualquer ajuda!