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.  
  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. int rawOffset = TimeZone.getTimeZone("America/Montreal").getRawOffset();
  35. int zo = z.getRules().getOffset( instant ).getTotalSeconds() * 1000;
  36.  
  37. System.out.println( "For ZoneId: " + z + " at instant: " + instant + " the ZoneOffset is: " + zo + "rawOffset: " + rawOffset );
  38.  
  39. }
  40. }
Success #stdin #stdout 0.39s 63316KB
stdin
Standard input is empty
stdout
ZoneId.systemDefault().toString(): GMT
odt: 2025-03-21T21:30:13.469930Z
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: -14400000rawOffset: -18000000