My country recently (in April) canceled daylight saving time for this year (maybe forever), but with the Java 8 Date Time API I'm still getting the old time with daylight saving.
The time in my computer is correct, tested it using the terminal:
date --date="2019-12-10"
ter dez 10 00:00:00 -03 2019
Although I'm aware I could solve the problem using ZoneId.of("-3")
, I'm looking for a better approach.
LocalDate.of(2019, 12, 10)
.atStartOfDay()
.atZone(ZoneId.of("America/Sao_Paulo"))
.toString()
The code above will output 2019-12-10T00:00-02:00[America/Sao_Paulo]
I'm expecting 2019-12-10T00:00-03:00[America/Sao_Paulo]
instead of 2019-12-10T00:00-02:00[America/Sao_Paulo]
.
It's worth saying that I'm running a Web Application in Jboss Wildfly, but I get the same results in a simple .jar file.
Full code I'm using for testing:
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.zone.ZoneRules;
public class Test {
public static void main(String... args) {
System.out.println(LocalDate.of(2019, 12, 10).atStartOfDay().atZone(ZoneId.of("-3")).toString());
System.out.println(LocalDate.of(2019, 12, 10).atStartOfDay().atZone(ZoneId.of("America/Sao_Paulo")).toString());
System.out.println(LocalDate.of(2019, 12, 10).atStartOfDay().atZone(ZoneId.systemDefault()).toString());
}
}
Output:
> javac Test.java
> java Test
2019-12-10T00:00-03:00
2019-12-10T00:00-02:00[America/Sao_Paulo]
2019-12-10T00:00-02:00[America/Sao_Paulo]
What should I do?