Skip to content

Commit

Permalink
feat(localization): 🎸 time zones, translated
Browse files Browse the repository at this point in the history
Refers: #5
  • Loading branch information
rcmoutinho committed Sep 11, 2019
1 parent b4ccad5 commit b0b94e4
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 85 deletions.
111 changes: 54 additions & 57 deletions book/03-localization/sections/05-time-zones.asc
Original file line number Diff line number Diff line change
@@ -1,72 +1,70 @@
:java-package: src/org/j6toj8/localization
:section-java-package: ../../../{java-package}

=== Fusos Horários
=== Time zones

.Objetivo
.Objective
--------------------------------------------------
Work with dates and times across time zones and manage changes resulting from daylight savings.
-
Trabalhe com datas e horas entre fusos horários e gerencie alterações resultantes de horários de verão.
--------------------------------------------------

O exame de certificação espera que você consiga lidar com diferentes fusos horários, além de conseguir entender o que ocorre ao realizar operações com datas e horas que passam por um horário de verão.
The certification exam expects you to be able to cope with different time zones, as well as understanding what happens when you perform operations with dates and times that go through daylight saving time.

Nessa seção será apresenta a classe `ZonedDateTime`, que armazena Data, Hora e um fuso horário. Ou seja, é muito parecida com a classe `LocalDateTime`, porém possui um fuso horário.
This section will introduce the `ZonedDateTime` class, which stores Date, Time and a time zone. That is, it is very similar to the `LocalDateTime` class, but has a time zone.

A representação de um `ZonedDateTime` é `2007-12-03T10:15:30+01:00 Europe/Paris`.
The representation of a `ZonedDateTime` is `2007-12-03T10:15:30+01:00 Europe/Paris`.

. É possível criar um `ZonedDateTime` através do método `static` chamado `now`.
. You can create a `ZonedDateTime` via the `static` method called `now`.
+
[source,java,indent=0]
.{java-package}/timezones/ZonedDateTime_Now.java
----
include::{section-java-package}/timezones/ZonedDateTime_Now.java[tag=code]
----
+
.Saída no console
.console output
[source,console]
----
2019-06-09T18:10:08.863-03:00[America/Sao_Paulo]
----
+
A saída no console irá apresentar a data e hora atual, e o fuso horário de onde o código está sendo executado.
The console output will display the current date and time, and the time zone from which the code is being executed.

. Também é possível criar um `ZonedDateTime` através do método `static` chamado `of`.
. It is also possible to create a `ZonedDateTime` through the `static` method called `of`.
+
[source,java,indent=0]
.{java-package}/timezones/ZonedDateTime_Of.java
----
include::{section-java-package}/timezones/ZonedDateTime_Of.java[tag=code]
----
+
.Saída no console
.console output
[source,console]
----
2019-06-09T13:20:03.000001-03:00[America/Sao_Paulo]
2019-06-09T13:20:03.000001-03:00[America/Sao_Paulo]
2019-06-09T13:20:03.000001-03:00[America/Sao_Paulo]
----
+
Perceba que é possível criar um `ZonedDateTime` diretamente, ou através de um `LocalDateTime`, ou com uma junção de um `LocalDate` e um `LocalTime`. Note que em todos os casos é necessário informar o fuso horário.
Note that it is possible to create a `ZonedDateTime` directly, either through a `LocalDateTime`, or with a join of a `LocalDate` and a `LocalTime`. Note that in all cases it is necessary to enter the time zone.

. Assim como todas a novas classes de data e hora, não é possível criar uma instância de `ZonedDateTime` utilizando o construtor.
. As with all new date and time classes, you cannot create an instance of `ZonedDateTime` using the constructor.
+
[source,java,indent=0]
.{java-package}/timezones/ZonedDateTime_Constructor.java
----
include::{section-java-package}/timezones/ZonedDateTime_Constructor.java[tag=code]
----

. Será lançada a exceção `DateTimeException` ao tentar criar uma data inválida.
. The `DateTimeException` exception will be thrown when trying to create an invalid date.
+
[source,java,indent=0]
.{java-package}/timezones/ZonedDateTime_Invalid.java
----
include::{section-java-package}/timezones/ZonedDateTime_Invalid.java[tag=code]
----
+
.Saída no console
.console output
[source,console]
----
Exception in thread "main" java.time.DateTimeException: Invalid date 'APRIL 31'
Expand All @@ -77,100 +75,99 @@ Exception in thread "main" java.time.DateTimeException: Invalid date 'APRIL 31'
at org.j6toj8.localization.timezones.ZonedDateTime_Invalid.main(ZonedDateTime_Invalid.java:10)
----

. Existem vários métodos para somar e subtrair de `ZonedDateTime`
. There are several methods for adding and subtracting from `ZonedDateTime`
+
[source,java,indent=0]
.{java-package}/timezones/ZonedDateTime_Manipulate.java
----
include::{section-java-package}/timezones/ZonedDateTime_Manipulate.java[tag=code]
----
+
.Saída no console
.console output
[source,console]
----
2019-05-20T09:20:12.000001-03:00[America/Sao_Paulo]
+2 horas: 2019-05-20T11:20:12.000001-03:00[America/Sao_Paulo]
+2 minutos: 2019-05-20T09:22:12.000001-03:00[America/Sao_Paulo]
+2 segundos: 2019-05-20T09:20:14.000001-03:00[America/Sao_Paulo]
+2 nanosegundos: 2019-05-20T09:20:12.000001002-03:00[America/Sao_Paulo]
+2 microssegundos: 2019-05-20T09:20:12.000003-03:00[America/Sao_Paulo]
+2 milissegundos: 2019-05-20T09:20:12.002001-03:00[America/Sao_Paulo]
-2 horas: 2019-05-20T07:20:12.000001-03:00[America/Sao_Paulo]
-2 minutos: 2019-05-20T09:18:12.000001-03:00[America/Sao_Paulo]
-2 segundos: 2019-05-20T09:20:10.000001-03:00[America/Sao_Paulo]
-2 nanosegundos: 2019-05-20T09:20:12.000000998-03:00[America/Sao_Paulo]
-2 microssegundos: 2019-05-20T09:20:11.999999-03:00[America/Sao_Paulo]
-2 milissegundos: 2019-05-20T09:20:11.998001-03:00[America/Sao_Paulo]
+2 dias: 2019-05-22T09:20:12.000001-03:00[America/Sao_Paulo]
+2 semanas: 2019-06-03T09:20:12.000001-03:00[America/Sao_Paulo]
+2 meses: 2019-07-20T09:20:12.000001-03:00[America/Sao_Paulo]
+2 anos: 2021-05-20T09:20:12.000001-03:00[America/Sao_Paulo]
+2 anos: 2021-05-20T09:20:12.000001-03:00[America/Sao_Paulo]
+2 décadas: 2039-05-20T09:20:12.000001-03:00[America/Sao_Paulo]
-2 dias: 2019-05-18T09:20:12.000001-03:00[America/Sao_Paulo]
-2 semanas: 2019-05-06T09:20:12.000001-03:00[America/Sao_Paulo]
-2 meses: 2019-03-20T09:20:12.000001-03:00[America/Sao_Paulo]
-2 anos: 2017-05-20T09:20:12.000001-03:00[America/Sao_Paulo]
-2 décadas: 1999-05-20T09:20:12.000001-03:00[America/Sao_Paulo]
----

. `ZonedDateTime` é imutável, então é necessário armazenar o retorno de uma alteração em uma variável.
+2 hours: 2019-05-20T11:20:12.000001-03:00[America/Sao_Paulo]
+2 minutes: 2019-05-20T09:22:12.000001-03:00[America/Sao_Paulo]
+2 seconds: 2019-05-20T09:20:14.000001-03:00[America/Sao_Paulo]
+2 nanoseconds: 2019-05-20T09:20:12.000001002-03:00[America/Sao_Paulo]
+2 microseconds: 2019-05-20T09:20:12.000003-03:00[America/Sao_Paulo]
+2 milliseconds: 2019-05-20T09:20:12.002001-03:00[America/Sao_Paulo]
-2 hours: 2019-05-20T07:20:12.000001-03:00[America/Sao_Paulo]
-2 minutes: 2019-05-20T09:18:12.000001-03:00[America/Sao_Paulo]
-2 seconds: 2019-05-20T09:20:10.000001-03:00[America/Sao_Paulo]
-2 nanoseconds: 2019-05-20T09:20:12.000000998-03:00[America/Sao_Paulo]
-2 microseconds: 2019-05-20T09:20:11.999999-03:00[America/Sao_Paulo]
-2 milliseconds: 2019-05-20T09:20:11.998001-03:00[America/Sao_Paulo]
+2 days: 2019-05-22T09:20:12.000001-03:00[America/Sao_Paulo]
+2 weeks: 2019-06-03T09:20:12.000001-03:00[America/Sao_Paulo]
+2 months: 2019-07-20T09:20:12.000001-03:00[America/Sao_Paulo]
+2 years: 2021-05-20T09:20:12.000001-03:00[America/Sao_Paulo]
+2 decades: 2039-05-20T09:20:12.000001-03:00[America/Sao_Paulo]
-2 days: 2019-05-18T09:20:12.000001-03:00[America/Sao_Paulo]
-2 weeks: 2019-05-06T09:20:12.000001-03:00[America/Sao_Paulo]
-2 months: 2019-03-20T09:20:12.000001-03:00[America/Sao_Paulo]
-2 years: 2017-05-20T09:20:12.000001-03:00[America/Sao_Paulo]
-2 decades: 1999-05-20T09:20:12.000001-03:00[America/Sao_Paulo]
----

. `ZonedDateTime` is immutable, so you must store the return of a change in a variable.
+
[source,java,indent=0]
.{java-package}/timezones/ZonedDateTime_Immutability.java
----
include::{section-java-package}/timezones/ZonedDateTime_Immutability.java[tag=code]
----
+
.Saída no console
.console output
[source,console]
----
2019-05-20T09:20:03.000000300-03:00[America/Sao_Paulo]
2019-05-20T09:20:03.000000300-03:00[America/Sao_Paulo]
2019-05-20T10:20:03.000000300-03:00[America/Sao_Paulo]
----

. É comum utilizar o encadeamento de chamadas com esses métodos.
. It is common to use call chaining with these methods.
+
[source,java,indent=0]
.{java-package}/timezones/ZonedDateTime_Chaining.java
----
include::{section-java-package}/timezones/ZonedDateTime_Chaining.java[tag=code]
----
+
.Saída no console
.console output
[source,console]
----
2019-05-20T09:20:04.000000300-03:00[America/Sao_Paulo]
2020-05-21T10:20:04.000000300-03:00[America/Sao_Paulo]
----

. Ao manipular um `ZonedDateTime`, será levado em conta o horário de verão daquele fuso horário.
. When handling a `ZonedDateTime`, the daylight saving time of that time zone will be taken into account.
+
[source,java,indent=0]
.{java-package}/timezones/ZonedDateTime_DaylightSavings.java
----
include::{section-java-package}/timezones/ZonedDateTime_DaylightSavings.java[tag=code]
----
+
.Saída no console
.console output
[source,console]
----
2018-11-03T23:30-03:00[America/Sao_Paulo]
+2 horas: 2018-11-04T02:30-02:00[America/Sao_Paulo]
+2 hours: 2018-11-04T02:30-02:00[America/Sao_Paulo]
----
+
Neste exemplo o fuso horário que era `-03:00` virou `-02:00`, pois esse foi o dia em que teve início o horário de verão no Brasil. Outro detalhe é que por conta do horário de verão, ao somar 2 horas às `23:30` resultou em `02:30` do dia seguinte. Se não houvesse horário de verão, o resultado seria `01:30`.
In this example, the time zone that was `-03:00` became `-02:00`, because that was beginning of the day daylight savings time in Brazil. Another detail is that because of daylight saving time, adding 2 hours at 23:30 resulted in 02:30 the next day. If there was no daylight saving time, the result would be `01: 30`.

. É possível recuperar todos os fusos horários disponíveis através da classe `ZoneId`.
. You can retrieve all available time zones through the `ZoneId` class.
+
[source,java,indent=0]
.{java-package}/timezones/ZonedDateTime_Zones.java
----
include::{section-java-package}/timezones/ZonedDateTime_Zones.java[tag=code]
----
+
.Saída no console (parcial)
.console output (parcial)
[source,console]
----
Asia/Aden
Expand All @@ -193,16 +190,16 @@ America/Guatemala
...
----
+
A lista do console irá apresentar todos os `ZoneId` disponíveis. O exemplo acima contempla apeanas parte dos `ZoneId`.
The console list will display all available `ZoneId`. The above example only contemplates part of `ZoneId`.
+
Além disso, existem muitos `ZoneId` duplicados, pois representam o mesmo fuso horário, como por exemplo `America/Sao_Paulo` e `Brazil/East`.
Also, there are many duplicate `ZoneId` as they represent the same time zone as for example `America/Sao_Paulo` and `Brazil/East`.

.Referências
.References
****
* Working with Dates and Times
+
Boyarsky, Jeanne; Selikoff, Scott. OCP: Oracle Certified Professional Java SE 8 Programmer II Study Guide (p. 234). Wiley. Edição do Kindle.
Boyarsky, Jeanne; Selikoff, Scott. OCP: Oracle Certified Professional Java SE 8 Programmer II Study Guide (p. 234). Wiley. Kindle Edition.
* https://www.baeldung.com/java-8-date-time-intro[Introduction to the Java 8 Date/Time API.]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ public class ZonedDateTime_Constructor {

public static void main(String[] args) {
// tag::code[]
ZonedDateTime zonedDateTime = new ZonedDateTime(); // NÃO COMPILA! - não possui construtor público
ZonedDateTime zonedDateTime = new ZonedDateTime(); // NOT COMPILING! - doesn't have a public constructor
System.out.println(zonedDateTime);
// end::code[]
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public static void main(String[] args) {
// tag::code[]
ZonedDateTime zonedDateTime = ZonedDateTime.of(2018, 11, 3, 23, 30, 0, 0, ZoneId.of("America/Sao_Paulo"));
System.out.println(zonedDateTime);
System.out.println("+2 horas: " + zonedDateTime.plusHours(2));
System.out.println("+2 hours: " + zonedDateTime.plusHours(2));
// end::code[]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ public static void main(String[] args) {
// tag::code[]
ZonedDateTime zonedDateTime = ZonedDateTime.of(2019, 5, 20, 9, 20, 3, 300, ZoneId.of("America/Sao_Paulo"));
System.out.println(zonedDateTime);
zonedDateTime.plusHours(1); // chamada perdida - a nova data/hora não foi armazenada em uma variável
zonedDateTime.plusHours(1); // missed call - new date/time was not stored in a variable
System.out.println(zonedDateTime);
zonedDateTime = zonedDateTime.plusHours(1); // chamada útil - data/hora armazenada na variável
zonedDateTime = zonedDateTime.plusHours(1); // useful call - date/time stored in variable
System.out.println(zonedDateTime);
// end::code[]
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public class ZonedDateTime_Invalid {

public static void main(String[] args) {
// tag::code[]
System.out.println(ZonedDateTime.of(2019, 4, 31, 9, 20, 3, 1000, ZoneId.of("America/Sao_Paulo"))); // lança exceção: não existe 31 de abril
System.out.println(ZonedDateTime.of(2019, 4, 31, 9, 20, 3, 1000, ZoneId.of("America/Sao_Paulo"))); // throws exception: there is no April 31
// end::code[]
}
}
45 changes: 22 additions & 23 deletions src/org/j6toj8/localization/timezones/ZonedDateTime_Manipulate.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,29 +10,28 @@ public static void main(String[] args) {
// tag::code[]
ZonedDateTime zonedDateTime = ZonedDateTime.of(2019, 5, 20, 9, 20, 12, 1000, ZoneId.of("America/Sao_Paulo"));
System.out.println(zonedDateTime);
System.out.println("+2 horas: " + zonedDateTime.plusHours(2));
System.out.println("+2 minutos: " + zonedDateTime.plusMinutes(2));
System.out.println("+2 segundos: " + zonedDateTime.plusSeconds(2));
System.out.println("+2 nanosegundos: " + zonedDateTime.plusNanos(2));
System.out.println("+2 microssegundos: " + zonedDateTime.plus(2, ChronoUnit.MICROS));
System.out.println("+2 milissegundos: " + zonedDateTime.plus(2, ChronoUnit.MILLIS));
System.out.println("-2 horas: " + zonedDateTime.minusHours(2));
System.out.println("-2 minutos: " + zonedDateTime.minusMinutes(2));
System.out.println("-2 segundos: " + zonedDateTime.minusSeconds(2));
System.out.println("-2 nanosegundos: " + zonedDateTime.minusNanos(2));
System.out.println("-2 microssegundos: " + zonedDateTime.minus(2, ChronoUnit.MICROS));
System.out.println("-2 milissegundos: " + zonedDateTime.minus(2, ChronoUnit.MILLIS));
System.out.println("+2 dias: " + zonedDateTime.plusDays(2));
System.out.println("+2 semanas: " + zonedDateTime.plusWeeks(2));
System.out.println("+2 meses: " + zonedDateTime.plusMonths(2));
System.out.println("+2 anos: " + zonedDateTime.plusYears(2));
System.out.println("+2 anos: " + zonedDateTime.plusYears(2));
System.out.println("+2 décadas: " + zonedDateTime.plus(2, ChronoUnit.DECADES));
System.out.println("-2 dias: " + zonedDateTime.minusDays(2));
System.out.println("-2 semanas: " + zonedDateTime.minusWeeks(2));
System.out.println("-2 meses: " + zonedDateTime.minusMonths(2));
System.out.println("-2 anos: " + zonedDateTime.minusYears(2));
System.out.println("-2 décadas: " + zonedDateTime.minus(2, ChronoUnit.DECADES));
System.out.println("+2 hours: " + zonedDateTime.plusHours(2));
System.out.println("+2 minutes: " + zonedDateTime.plusMinutes(2));
System.out.println("+2 seconds: " + zonedDateTime.plusSeconds(2));
System.out.println("+2 nanoseconds: " + zonedDateTime.plusNanos(2));
System.out.println("+2 microseconds: " + zonedDateTime.plus(2, ChronoUnit.MICROS));
System.out.println("+2 milliseconds: " + zonedDateTime.plus(2, ChronoUnit.MILLIS));
System.out.println("-2 hours: " + zonedDateTime.minusHours(2));
System.out.println("-2 minutes: " + zonedDateTime.minusMinutes(2));
System.out.println("-2 seconds: " + zonedDateTime.minusSeconds(2));
System.out.println("-2 nanoseconds: " + zonedDateTime.minusNanos(2));
System.out.println("-2 microseconds: " + zonedDateTime.minus(2, ChronoUnit.MICROS));
System.out.println("-2 milliseconds: " + zonedDateTime.minus(2, ChronoUnit.MILLIS));
System.out.println("+2 days: " + zonedDateTime.plusDays(2));
System.out.println("+2 weeks: " + zonedDateTime.plusWeeks(2));
System.out.println("+2 months: " + zonedDateTime.plusMonths(2));
System.out.println("+2 years: " + zonedDateTime.plusYears(2));
System.out.println("+2 decades: " + zonedDateTime.plus(2, ChronoUnit.DECADES));
System.out.println("-2 days: " + zonedDateTime.minusDays(2));
System.out.println("-2 weeks: " + zonedDateTime.minusWeeks(2));
System.out.println("-2 months: " + zonedDateTime.minusMonths(2));
System.out.println("-2 years: " + zonedDateTime.minusYears(2));
System.out.println("-2 decades: " + zonedDateTime.minus(2, ChronoUnit.DECADES));
// end::code[]
}
}

0 comments on commit b0b94e4

Please sign in to comment.