Skip to content

Instantly share code, notes, and snippets.

@jonyfs
Created June 21, 2018 15:59
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jonyfs/9964cd9f46f694d949d337b4db54a33f to your computer and use it in GitHub Desktop.
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
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