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.format.* ;
  9.  
  10. /* Name of the class has to be "Main" only if the class is public. */
  11. class Ideone
  12. {
  13. public static void main (String[] args) throws java.lang.Exception
  14. {
  15. Instant epoch = Instant.EPOCH ;
  16.  
  17. ZoneId z = ZoneId.of( "Europe/London" ) ;
  18. ZonedDateTime zdt = epoch.atZone( z ) ;
  19.  
  20. System.out.println( "epoch.toString(): " + epoch ) ;
  21. System.out.println( "zdt.toString(): " + zdt ) ;
  22.  
  23. ZonedDateTime zdt2019 =
  24. Instant
  25. .parse( "2019-01-01T00:00:00Z" )
  26. .atZone( ZoneId.of( "Europe/London" ) )
  27. ;
  28.  
  29. System.out.println( "zdt2019.toString(): " + zdt2019 ) ;
  30.  
  31. // Extract `Instant` from our `ZonedDateTime`, effectively adjusting from zone to UTC.
  32. Instant instant = zdt.toInstant() ;
  33.  
  34. System.out.println( "instant.toString(): " + instant ) ;
  35.  
  36. java.util.Date d = Date.from( instant ) ; // Same moment, but with possible data-loss as nanoseconds are truncated to milliseconds.
  37.  
  38. System.out.println( "d.toString(): " + d ) ;
  39.  
  40. }
  41. }
Success #stdin #stdout 0.21s 2184192KB
stdin
Standard input is empty
stdout
epoch.toString(): 1970-01-01T00:00:00Z
zdt.toString(): 1970-01-01T01:00+01:00[Europe/London]
zdt2019.toString(): 2019-01-01T00:00Z[Europe/London]
instant.toString(): 1970-01-01T00:00:00Z
d.toString(): Thu Jan 01 00:00:00 GMT 1970