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.*;
  8.  
  9. /* Name of the class has to be "Main" only if the class is public. */
  10. class Ideone
  11. {
  12. public static void main (String[] args) throws java.lang.Exception
  13. {
  14.  
  15. OffsetDateTime odt = OffsetDateTime.now ( ZoneId.systemDefault () );
  16. ZoneOffset zoneOffsetOfOdt = odt.getOffset ();
  17.  
  18. System.out.println( "ZoneId.systemDefault().toString(): " + ZoneId.systemDefault ().toString() );
  19. System.out.println( "odt: " + odt );
  20. System.out.println( "zoneOffsetOfOdt: " + zoneOffsetOfOdt );
  21. System.out.println( "-------------------- " );
  22.  
  23. ZoneId z = ZoneId.of( "America/Montreal" );
  24. LocalDate ld = LocalDate.of( 2017 , 4 , 25 );
  25. ZonedDateTime zdtXmas = ld.atStartOfDay( z );
  26. ZoneOffset zoneOffsetXmas = zdtXmas.getOffset();
  27.  
  28. System.out.println( "zdtXmas.toString(): " + zdtXmas );
  29. System.out.println( "zoneOffsetXmas.toString(): " + zoneOffsetXmas );
  30. System.out.println( "zoneOffsetXmas.getTotalSeconds(): " + zoneOffsetXmas.getTotalSeconds() );
  31. System.out.println( "-------------------- " );
  32.  
  33. Instant instant = zdtXmas.toInstant();
  34. ZoneOffset zo = z.getRules().getOffset( instant );
  35.  
  36. System.out.println( "For ZoneId: " + z + " at instant: " + instant + " the ZoneOffset is: " + zo );
  37.  
  38. }
  39. }
Success #stdin #stdout 0.37s 61208KB
stdin
Standard input is empty
stdout
ZoneId.systemDefault().toString(): GMT
odt: 2025-03-21T20:55:47.019847Z
zoneOffsetOfOdt: Z
-------------------- 
zdtXmas.toString(): 2017-04-25T00:00-04:00[America/Montreal]
zoneOffsetXmas.toString(): -04:00
zoneOffsetXmas.getTotalSeconds(): -14400
-------------------- 
For ZoneId: America/Montreal at instant: 2017-04-25T04:00:00Z the ZoneOffset is: -04:00