fork download
  1. import java.time.*;
  2. import java.time.format.DateTimeFormatter;
  3. import java.util.Locale;
  4.  
  5. public class Main {
  6. public static void main(String[] args) {
  7. // ZoneId.systemDefault() returns the ZoneId of the JVM executing the code.
  8. // Replace it with the applicable ZoneId e.g. ZoneId.of("America/New_York")
  9. ZonedDateTime now = ZonedDateTime.now(ZoneId.systemDefault());
  10.  
  11. // Default format
  12. System.out.println(now);
  13.  
  14. // Custom format
  15. DateTimeFormatter formatter = DateTimeFormatter.ofPattern("uuuu.MM.dd HH:mm:ss z", Locale.ENGLISH);
  16. System.out.println(now.format(formatter));
  17. }
  18. }
Success #stdin #stdout 0.15s 57796KB
stdin
Standard input is empty
stdout
2024-10-17T21:36:48.202859Z[GMT]
2024.10.17 21:36:48 GMT