Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.