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. import java.time.temporal.*;
  10. import java.time.zone.*;
  11. import java.util.concurrent.*;
  12.  
  13. /* Name of the class has to be "Main" only if the class is public. */
  14. class Ideone
  15. {
  16. public static void main (String[] args) throws java.lang.Exception
  17. {
  18.  
  19. Instant instant = Instant.now ();
  20. try {
  21. Thread.sleep ( 3_500L ); // Wait about three and a half seconds.
  22. } catch ( InterruptedException ex ) {
  23. System.out.println ( "Thread sleep interrupted." );
  24. }
  25. Instant instantLater = Instant.now ();
  26. Duration duration = Duration.between ( instant , instantLater );
  27.  
  28. ZoneId z = ZoneId.of( "America/Montreal" );
  29. ZonedDateTime zdt = instant.atZone( z );
  30.  
  31. System.out.println ( "instant.toString()/instantLater.toString(): " + instant + "/" + instantLater );
  32. System.out.println ( "duration.toString(): " + duration );
  33. System.out.println ( "zdt.toString(): " + zdt );
  34. }
  35. }
Success #stdin #stdout 0.17s 4386816KB
stdin
Standard input is empty
stdout
instant.toString()/instantLater.toString(): 2017-02-27T14:48:58.664Z/2017-02-27T14:49:02.164Z
duration.toString(): PT3.5S
zdt.toString(): 2017-02-27T09:48:58.664-05:00[America/Montreal]