11:java-package: src/org/j6toj8/localization
22:section-java-package: ../../../{java-package}
33
4- === Fusos Horários
4+ === Time zones
55
6- .Objetivo
6+ .Objective
77--------------------------------------------------
88Work with dates and times across time zones and manage changes resulting from daylight savings.
9- -
10- Trabalhe com datas e horas entre fusos horários e gerencie alterações resultantes de horários de verão.
119--------------------------------------------------
1210
13- 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 .
11+ 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 .
1412
15- 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 .
13+ 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 .
1614
17- A representação de um `ZonedDateTime` é `2007-12-03T10:15:30+01:00 Europe/Paris`.
15+ The representation of a `ZonedDateTime` is `2007-12-03T10:15:30+01:00 Europe/Paris`.
1816
19- . É possível criar um `ZonedDateTime` através do método `static` chamado `now`.
17+ . You can create a `ZonedDateTime` via the `static` method called `now`.
2018+
2119[source,java,indent=0]
2220.{java-package}/timezones/ZonedDateTime_Now.java
2321----
2422include::{section-java-package}/timezones/ZonedDateTime_Now.java[tag=code]
2523----
2624+
27- .Saída no console
25+ .console output
2826[source,console]
2927----
30282019-06-09T18:10:08.863-03:00[America/Sao_Paulo]
3129----
3230+
33- 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 .
31+ The console output will display the current date and time, and the time zone from which the code is being executed .
3432
35- . Também é possível criar um `ZonedDateTime` através do método `static` chamado `of`.
33+ . It is also possible to create a `ZonedDateTime` through the `static` method called `of`.
3634+
3735[source,java,indent=0]
3836.{java-package}/timezones/ZonedDateTime_Of.java
3937----
4038include::{section-java-package}/timezones/ZonedDateTime_Of.java[tag=code]
4139----
4240+
43- .Saída no console
41+ .console output
4442[source,console]
4543----
46442019-06-09T13:20:03.000001-03:00[America/Sao_Paulo]
47452019-06-09T13:20:03.000001-03:00[America/Sao_Paulo]
48462019-06-09T13:20:03.000001-03:00[America/Sao_Paulo]
4947----
5048+
51- 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 .
49+ 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 .
5250
53- . Assim como todas a novas classes de data e hora, não é possível criar uma instância de `ZonedDateTime` utilizando o construtor .
51+ . As with all new date and time classes, you cannot create an instance of `ZonedDateTime` using the constructor .
5452+
5553[source,java,indent=0]
5654.{java-package}/timezones/ZonedDateTime_Constructor.java
5755----
5856include::{section-java-package}/timezones/ZonedDateTime_Constructor.java[tag=code]
5957----
6058
61- . Será lançada a exceção `DateTimeException` ao tentar criar uma data inválida .
59+ . The `DateTimeException` exception will be thrown when trying to create an invalid date .
6260+
6361[source,java,indent=0]
6462.{java-package}/timezones/ZonedDateTime_Invalid.java
6563----
6664include::{section-java-package}/timezones/ZonedDateTime_Invalid.java[tag=code]
6765----
6866+
69- .Saída no console
67+ .console output
7068[source,console]
7169----
7270Exception in thread "main" java.time.DateTimeException: Invalid date 'APRIL 31'
@@ -77,100 +75,99 @@ Exception in thread "main" java.time.DateTimeException: Invalid date 'APRIL 31'
7775 at org.j6toj8.localization.timezones.ZonedDateTime_Invalid.main(ZonedDateTime_Invalid.java:10)
7876----
7977
80- . Existem vários métodos para somar e subtrair de `ZonedDateTime`
78+ . There are several methods for adding and subtracting from `ZonedDateTime`
8179+
8280[source,java,indent=0]
8381.{java-package}/timezones/ZonedDateTime_Manipulate.java
8482----
8583include::{section-java-package}/timezones/ZonedDateTime_Manipulate.java[tag=code]
8684----
8785+
88- .Saída no console
86+ .console output
8987[source,console]
9088----
91892019-05-20T09:20:12.000001-03:00[America/Sao_Paulo]
92- +2 horas: 2019-05-20T11:20:12.000001-03:00[America/Sao_Paulo]
93- +2 minutos: 2019-05-20T09:22:12.000001-03:00[America/Sao_Paulo]
94- +2 segundos: 2019-05-20T09:20:14.000001-03:00[America/Sao_Paulo]
95- +2 nanosegundos: 2019-05-20T09:20:12.000001002-03:00[America/Sao_Paulo]
96- +2 microssegundos: 2019-05-20T09:20:12.000003-03:00[America/Sao_Paulo]
97- +2 milissegundos: 2019-05-20T09:20:12.002001-03:00[America/Sao_Paulo]
98- -2 horas: 2019-05-20T07:20:12.000001-03:00[America/Sao_Paulo]
99- -2 minutos: 2019-05-20T09:18:12.000001-03:00[America/Sao_Paulo]
100- -2 segundos: 2019-05-20T09:20:10.000001-03:00[America/Sao_Paulo]
101- -2 nanosegundos: 2019-05-20T09:20:12.000000998-03:00[America/Sao_Paulo]
102- -2 microssegundos: 2019-05-20T09:20:11.999999-03:00[America/Sao_Paulo]
103- -2 milissegundos: 2019-05-20T09:20:11.998001-03:00[America/Sao_Paulo]
104- +2 dias: 2019-05-22T09:20:12.000001-03:00[America/Sao_Paulo]
105- +2 semanas: 2019-06-03T09:20:12.000001-03:00[America/Sao_Paulo]
106- +2 meses: 2019-07-20T09:20:12.000001-03:00[America/Sao_Paulo]
107- +2 anos: 2021-05-20T09:20:12.000001-03:00[America/Sao_Paulo]
108- +2 anos: 2021-05-20T09:20:12.000001-03:00[America/Sao_Paulo]
109- +2 décadas: 2039-05-20T09:20:12.000001-03:00[America/Sao_Paulo]
110- -2 dias: 2019-05-18T09:20:12.000001-03:00[America/Sao_Paulo]
111- -2 semanas: 2019-05-06T09:20:12.000001-03:00[America/Sao_Paulo]
112- -2 meses: 2019-03-20T09:20:12.000001-03:00[America/Sao_Paulo]
113- -2 anos: 2017-05-20T09:20:12.000001-03:00[America/Sao_Paulo]
114- -2 décadas: 1999-05-20T09:20:12.000001-03:00[America/Sao_Paulo]
115- ----
116-
117- . `ZonedDateTime` é imutável, então é necessário armazenar o retorno de uma alteração em uma variável.
90+ +2 hours: 2019-05-20T11:20:12.000001-03:00[America/Sao_Paulo]
91+ +2 minutes: 2019-05-20T09:22:12.000001-03:00[America/Sao_Paulo]
92+ +2 seconds: 2019-05-20T09:20:14.000001-03:00[America/Sao_Paulo]
93+ +2 nanoseconds: 2019-05-20T09:20:12.000001002-03:00[America/Sao_Paulo]
94+ +2 microseconds: 2019-05-20T09:20:12.000003-03:00[America/Sao_Paulo]
95+ +2 milliseconds: 2019-05-20T09:20:12.002001-03:00[America/Sao_Paulo]
96+ -2 hours: 2019-05-20T07:20:12.000001-03:00[America/Sao_Paulo]
97+ -2 minutes: 2019-05-20T09:18:12.000001-03:00[America/Sao_Paulo]
98+ -2 seconds: 2019-05-20T09:20:10.000001-03:00[America/Sao_Paulo]
99+ -2 nanoseconds: 2019-05-20T09:20:12.000000998-03:00[America/Sao_Paulo]
100+ -2 microseconds: 2019-05-20T09:20:11.999999-03:00[America/Sao_Paulo]
101+ -2 milliseconds: 2019-05-20T09:20:11.998001-03:00[America/Sao_Paulo]
102+ +2 days: 2019-05-22T09:20:12.000001-03:00[America/Sao_Paulo]
103+ +2 weeks: 2019-06-03T09:20:12.000001-03:00[America/Sao_Paulo]
104+ +2 months: 2019-07-20T09:20:12.000001-03:00[America/Sao_Paulo]
105+ +2 years: 2021-05-20T09:20:12.000001-03:00[America/Sao_Paulo]
106+ +2 decades: 2039-05-20T09:20:12.000001-03:00[America/Sao_Paulo]
107+ -2 days: 2019-05-18T09:20:12.000001-03:00[America/Sao_Paulo]
108+ -2 weeks: 2019-05-06T09:20:12.000001-03:00[America/Sao_Paulo]
109+ -2 months: 2019-03-20T09:20:12.000001-03:00[America/Sao_Paulo]
110+ -2 years: 2017-05-20T09:20:12.000001-03:00[America/Sao_Paulo]
111+ -2 decades: 1999-05-20T09:20:12.000001-03:00[America/Sao_Paulo]
112+ ----
113+
114+ . `ZonedDateTime` is immutable, so you must store the return of a change in a variable.
118115+
119116[source,java,indent=0]
120117.{java-package}/timezones/ZonedDateTime_Immutability.java
121118----
122119include::{section-java-package}/timezones/ZonedDateTime_Immutability.java[tag=code]
123120----
124121+
125- .Saída no console
122+ .console output
126123[source,console]
127124----
1281252019-05-20T09:20:03.000000300-03:00[America/Sao_Paulo]
1291262019-05-20T09:20:03.000000300-03:00[America/Sao_Paulo]
1301272019-05-20T10:20:03.000000300-03:00[America/Sao_Paulo]
131128----
132129
133- . É comum utilizar o encadeamento de chamadas com esses métodos .
130+ . It is common to use call chaining with these methods .
134131+
135132[source,java,indent=0]
136133.{java-package}/timezones/ZonedDateTime_Chaining.java
137134----
138135include::{section-java-package}/timezones/ZonedDateTime_Chaining.java[tag=code]
139136----
140137+
141- .Saída no console
138+ .console output
142139[source,console]
143140----
1441412019-05-20T09:20:04.000000300-03:00[America/Sao_Paulo]
1451422020-05-21T10:20:04.000000300-03:00[America/Sao_Paulo]
146143----
147144
148- . Ao manipular um `ZonedDateTime`, será levado em conta o horário de verão daquele fuso horário .
145+ . When handling a `ZonedDateTime`, the daylight saving time of that time zone will be taken into account .
149146+
150147[source,java,indent=0]
151148.{java-package}/timezones/ZonedDateTime_DaylightSavings.java
152149----
153150include::{section-java-package}/timezones/ZonedDateTime_DaylightSavings.java[tag=code]
154151----
155152+
156- .Saída no console
153+ .console output
157154[source,console]
158155----
1591562018-11-03T23:30-03:00[America/Sao_Paulo]
160- +2 horas : 2018-11-04T02:30-02:00[America/Sao_Paulo]
157+ +2 hours : 2018-11-04T02:30-02:00[America/Sao_Paulo]
161158----
162159+
163- 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`.
160+ 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`.
164161
165- . É possível recuperar todos os fusos horários disponíveis através da classe `ZoneId`.
162+ . You can retrieve all available time zones through the `ZoneId` class .
166163+
167164[source,java,indent=0]
168165.{java-package}/timezones/ZonedDateTime_Zones.java
169166----
170167include::{section-java-package}/timezones/ZonedDateTime_Zones.java[tag=code]
171168----
172169+
173- .Saída no console (parcial)
170+ .console output (parcial)
174171[source,console]
175172----
176173Asia/Aden
@@ -193,16 +190,16 @@ America/Guatemala
193190...
194191----
195192+
196- A lista do console irá apresentar todos os `ZoneId` disponíveis. O exemplo acima contempla apeanas parte dos `ZoneId`.
193+ The console list will display all available `ZoneId`. The above example only contemplates part of `ZoneId`.
197194+
198- Além disso, existem muitos `ZoneId` duplicados, pois representam o mesmo fuso horário, como por exemplo `America/Sao_Paulo` e `Brazil/East`.
195+ Also, there are many duplicate `ZoneId` as they represent the same time zone as for example `America/Sao_Paulo` and `Brazil/East`.
199196
200- .Referências
197+ .References
201198****
202199
203200* Working with Dates and Times
204201+
205- Boyarsky, Jeanne; Selikoff, Scott. OCP: Oracle Certified Professional Java SE 8 Programmer II Study Guide (p. 234). Wiley. Edição do Kindle.
202+ Boyarsky, Jeanne; Selikoff, Scott. OCP: Oracle Certified Professional Java SE 8 Programmer II Study Guide (p. 234). Wiley. Kindle Edition.
206203
207204* https://www.baeldung.com/java-8-date-time-intro[Introduction to the Java 8 Date/Time API.]
208205
0 commit comments