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.temporal.*;
  9. import java.time.format.*;
  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. // ZonedDateTime
  17. LocalDate ld = LocalDate.of( 2017 , Month.MARCH , 12 ) ;
  18. LocalTime lt = LocalTime.of( 1 , 0 ) ;
  19. ZoneId z = ZoneId.of( "America/New_York" ) ;
  20. ZonedDateTime zdt = ZonedDateTime.of( ld , lt , z ) ;
  21. ZonedDateTime zdtOneHourLater = zdt.plusHours( 1 ) ;
  22.  
  23. System.out.println( "zdt: " + zdt ) ;
  24. System.out.println( "zdtOneHourLater: " + zdtOneHourLater ) ;
  25. System.out.println( "Yikes! 1 AM plus an hour is 3 AM? Yes, that is an anomaly known as Daylight Saving Time (DST)." ) ;
  26. System.out.println( "" ) ;
  27.  
  28. // Instant
  29. Instant instant = zdt.toInstant() ; // Adjust into UTC. Same moment, same point on the timeline, but viewed by a different wall-clock.
  30. Instant instantOneHourLater = instant.plus( 1 , ChronoUnit.HOURS ) ;
  31.  
  32. System.out.println( "instant: " + instant ) ;
  33. System.out.println( "instantOneHourLater: " + instantOneHourLater ) ;
  34. System.out.println( "Instant is always in UTC. So no anomalies, no DST. Adding an hour to 1 AM results in 2 AM every time." ) ;
  35. }
  36. }
Success #stdin #stdout 0.24s 2841600KB
stdin
Standard input is empty
stdout
zdt: 2017-03-12T01:00-05:00[America/New_York]
zdtOneHourLater: 2017-03-12T03:00-04:00[America/New_York]
Yikes! 1 AM plus an hour is 3 AM? Yes, that is an anomaly known as Daylight Saving Time (DST).

instant: 2017-03-12T06:00:00Z
instantOneHourLater: 2017-03-12T07:00:00Z
Instant is always in UTC. So no anomalies, no DST. Adding an hour to 1 AM results in 2 AM every time.