fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. import java.time.*;
  8. import java.time.temporal.*;
  9. import java.time.format.*;
  10.  
  11. /* Name of the class has to be "Main" only if the class is public. */
  12. class Ideone
  13. {
  14. public static void main (String[] args) throws java.lang.Exception
  15. {
  16.  
  17. Instant instant = Instant.now(); // Current moment in UTC
  18. ZoneId z = ZoneId.of( "Pacific/Auckland" );
  19. ZonedDateTime zdt = instant.atZone( z ); // Same moment viewed as wall-clock time of particular region.
  20.  
  21. DateTimeFormatter f = DateTimeFormatter.ofPattern( "uuuu-M-d (HH:mm:ss.S)" , Locale.US );
  22. String output = zdt.format( f );
  23.  
  24. System.out.println( "instant.toString(): " + instant ); // Standard ISO 8601 format.
  25. System.out.println( "zdt.toString(): " + zdt ); // Standard ISO 8601 format.
  26. System.out.println( "output: " + output ); // Custom format.
  27.  
  28. }
  29. }
Success #stdin #stdout 0.16s 4386816KB
stdin
Standard input is empty
stdout
instant.toString(): 2017-04-15T13:52:32.268Z
zdt.toString(): 2017-04-16T01:52:32.268+12:00[Pacific/Auckland]
output: 2017-4-16 (01:52:32.2)