fork(1) 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.Instant ;
  8. import java.time.temporal.ChronoUnit ;
  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.  
  16. Instant myInstant = Instant.now() ;
  17. //Instant myInstant = Instant.now().truncatedTo( ChronoUnit.SECONDS ) ;
  18.  
  19. Instant myRoundedUpInstant =
  20. Optional.of(myInstant)
  21. .filter( t -> t.getNano() != 0 )
  22. .map (t -> t.truncatedTo( ChronoUnit.SECONDS ).plusSeconds( 1 ) )
  23. .orElse( myInstant ) ;
  24.  
  25. System.out.println( "myInstant.toString(): " + myInstant ) ;
  26. System.out.println( "myRoundedUpInstant(): " + myRoundedUpInstant ) ;
  27.  
  28. }
  29. }
Success #stdin #stdout 0.11s 36888KB
stdin
Standard input is empty
stdout
myInstant.toString(): 2019-07-30T20:11:11.769066Z
myRoundedUpInstant(): 2019-07-30T20:11:12Z