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.util.concurrent.*;
  11.  
  12. /* Name of the class has to be "Main" only if the class is public. */
  13. class Ideone
  14. {
  15. public static void main (String[] args) throws java.lang.Exception
  16. {
  17.  
  18. Instant start = Instant.now() ;
  19. Instant stop = start.plusSeconds( TimeUnit.MINUTES.toSeconds( 7L ) ) ;
  20.  
  21. Duration duration = Duration.between( start , stop );
  22.  
  23. String outputStandard = duration.toString();
  24.  
  25. Duration duration2 = Duration.parse( outputStandard ); // Going the other direction, parsing string.
  26.  
  27. System.out.println( "Start/Stop: " + start + "/" + stop );
  28. System.out.println( "outputStandard: " + outputStandard );
  29. System.out.println( "duration2.toString(): " + duration2 );
  30.  
  31.  
  32. }
  33. }
Success #stdin #stdout 0.2s 3359744KB
stdin
Standard input is empty
stdout
Start/Stop: 2017-02-25T22:42:53.526Z/2017-02-25T22:49:53.526Z
outputStandard: PT7M
duration2.toString(): PT7M