Created
June 21, 2018 15:59
-
-
Save jonyfs/9964cd9f46f694d949d337b4db54a33f to your computer and use it in GitHub Desktop.
Different java.time.ZonedDateTime formatted with pattern yyyy-MM-dd'T'HH:mm:ssXXX
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.time.LocalDateTime; | |
import java.time.ZoneId; | |
import java.time.ZonedDateTime; | |
import java.time.format.DateTimeFormatter; | |
public class TimeZoneTest { | |
public static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ssXXX"); | |
public static void main(String[] args) { | |
System.out.println("São Paulo: " + nowInSaoPaulo().format(formatter)); | |
System.out.println("China: " + nowInShangai().format(formatter)); | |
System.out.println("UTC: " + nowInUTC().format(formatter)); | |
} | |
public static ZonedDateTime nowInSaoPaulo() { | |
ZoneId zoneId = ZoneId.of("America/Sao_Paulo"); | |
System.out.println("São Paulo ZoneId: " + zoneId); | |
ZonedDateTime agoraSaoPaulo = ZonedDateTime.of(LocalDateTime.now(), zoneId); | |
return agoraSaoPaulo; | |
} | |
public static ZonedDateTime nowInShangai() { | |
ZoneId zoneId = ZoneId.of("Asia/Shanghai"); | |
System.out.println("Shangai ZoneId: " + zoneId); | |
ZonedDateTime agoraSaoPaulo = ZonedDateTime.of(LocalDateTime.now(), zoneId); | |
return agoraSaoPaulo; | |
} | |
public static ZonedDateTime nowInUTC() { | |
ZoneId zoneId = ZoneId.of("UTC"); | |
System.out.println("Shangai ZoneId: " + zoneId); | |
ZonedDateTime agoraSaoPaulo = ZonedDateTime.of(LocalDateTime.now(), zoneId); | |
return agoraSaoPaulo; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment