Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

218
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

0

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

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda