Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

220
Visualizações
HOUR_OF_DAY sets the same hour for value 0 and 1

I have a tricky (at least for me) question with java time zones. I will start with an example that works as expected:

TimeZone tz = TimeZone.getDefault();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat();
simpleDateFormat.setTimeZone(tz);
Calendar calendar = Calendar.getInstance();
calendar.setTimeZone(tz);

calendar.set(Calendar.HOUR_OF_DAY, 0);
System.out.println("0: " + simpleDateFormat.format(calendar.getTime()));

calendar.set(Calendar.HOUR_OF_DAY, 1);
System.out.println("1: " + simpleDateFormat.format(calendar.getTime()));

calendar.set(Calendar.HOUR_OF_DAY, 2);
System.out.println("2: " + simpleDateFormat.format(calendar.getTime()));

output:

0 4/28/17 12:27 AM
1: 4/28/17 1:27 AM
2: 4/28/17 2:27 AM

And now a little of magic:

TimeZone tz = TimeZone.getTimeZone("Africa/Cairo");
SimpleDateFormat simpleDateFormat = new SimpleDateFormat();
simpleDateFormat.setTimeZone(tz);
Calendar calendar = Calendar.getInstance();
calendar.setTimeZone(tz);

calendar.set(Calendar.HOUR_OF_DAY, 0);
System.out.println("0: " + simpleDateFormat.format(calendar.getTime()));

calendar.set(Calendar.HOUR_OF_DAY, 1);
System.out.println("1: " + simpleDateFormat.format(calendar.getTime()));

calendar.set(Calendar.HOUR_OF_DAY, 2);
System.out.println("2: " + simpleDateFormat.format(calendar.getTime()));

output:

0: 4/28/17 1:31 AM
1: 4/28/17 1:31 AM
2: 4/28/17 2:31 AM

Could someone explain me why HOUR_OF_DAY for values 0 and 1 set hour on the same value for specific TimeZone's?

over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

Your machine has an old version of time zone data, in which Egypt would have put the clocks forward at midnight local time at the start of April 28th 2017. Therefore 12:31am wouldn't have existed, and Java is skipping forward by an hour instead1.

On July 5th 2016, IANA time zone data 2016f was released with the change to the Africa/Cairo time zone to not observe DST from that point onwards. (This was a very late announcement, as the next change would have been July 8th 2016.)

You can see all the transitions for every IANA time zone, across different versions of the IANA data, at http://nodatime.github.io/tzvalidate/. (Disclaimer: I build that page. I started the tzvalidate project to validate that different date/time libraries understand the time zone data in the same way.)


1 I would argue that the API should force you to state what you want to happen in that case, but I won't go into everything that's wrong with java.util.Calendar here...

over 4 years ago · Santiago Trujillo Relatório

0

A few years ago, that timezone was planned to have a DST transition the night of the 28th of April 2017: at 00:00am, clocks would have been moved forward by one hour. So a time between midnight and 1am would have been invalid due to the transition.

However that transition did not occur in practice because it was cancelled (Egypt seem to have cancelled and reinstated DST a few times over the past few years).

In conclusion, you are probably using an older version of Java which has an outdated timezone database.

over 4 years ago · Santiago Trujillo Relatório

0

You were right. Problem was with time transition at 28th. Today everything works fine :)

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda