fork(2) 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.  
  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. OffsetDateTime odt = OffsetDateTime.now() ;
  17. OffsetDateTime odtTruncated = OffsetDateTime.now().truncatedTo( ChronoUnit.MILLIS ) ;
  18.  
  19. System.out.println( "odt.toString(): " + odt ) ;
  20. System.out.println( "odtTruncated.toString(): " + odtTruncated ) ;
  21.  
  22. Instant instant = Instant.now() ; // Current moment in UTC.
  23. ZoneOffset offset = ZoneId.of( "Asia/Kolkata" ).getRules().getOffset( instant ) ; // We must pass a moment. India is currently at five and a half hours ahead of UTC. But it has not always been so in the past, and may not always be so in the future.
  24. OffsetDateTime odtKolkata = instant.atOffset( offset ) ;
  25.  
  26. System.out.println( "odtKolkata.toString(): " + odtKolkata ) ;
  27.  
  28. String output = ZonedDateTime.now( ZoneId.of( "Asia/Kolkata" ) ).toOffsetDateTime().toString() ;
  29. System.out.println( "output: " + output ) ;
  30. }
  31. }
Success #stdin #stdout 0.14s 39876KB
stdin
Standard input is empty
stdout
odt.toString(): 2019-08-22T17:33:18.995839Z
odtTruncated.toString(): 2019-08-22T17:33:18.995Z
odtKolkata.toString(): 2019-08-22T23:03:19.026221+05:30
output: 2019-08-22T23:03:19.072681+05:30