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.  
  8. import java.time.*;
  9. import java.time.temporal.*;
  10. import java.time.format.*;
  11.  
  12.  
  13.  
  14. /* Name of the class has to be "Main" only if the class is public. */
  15. class Ideone
  16. {
  17. public static void main (String[] args) throws java.lang.Exception
  18. {
  19.  
  20. LocalDate ld = LocalDate.of ( 2015, Month.OCTOBER, 24 ); // 24th Oct 2015 at 10:00am
  21. LocalTime lt = LocalTime.of ( 10, 0 );
  22. ZoneId z = ZoneId.of ( "Europe/London" );
  23. ZonedDateTime zdtStart = ZonedDateTime.of ( ld, lt, z );
  24. Instant instantStart = zdtStart.toInstant ( );
  25. Duration sixHours = Duration.ofHours ( 6 );
  26.  
  27.  
  28. // Increment the `ZonedDateTime`.
  29. System.out.println ( "\n--------| Incrementing ZonedDateTime |-------------------" );
  30. ZonedDateTime zdt = zdtStart;
  31. for ( int i = 1 ; i <= 10 ; i++ ) {
  32. System.out.println ( "zdt.toString() " + zdt + " | zdt.toInstant().toString(): " + zdt.toInstant ( ) );
  33. // Set up next loop.
  34. zdt = zdt.plus ( sixHours );
  35. }
  36.  
  37. // Increment the `Instant`.
  38. System.out.println ( "\n--------| Incrementing Instant |-------------------" );
  39. Instant instant = instantStart;
  40. for ( int i = 1 ; i <= 10 ; i++ ) {
  41. System.out.println ( ">instant.toString() " + instant + " | instant.atZone(z).toString(): " + instant.atZone ( z ) );
  42. // Set up next loop.
  43. instant = instant.plus ( sixHours );
  44. }
  45.  
  46. }
  47. }
Success #stdin #stdout 0.17s 4386816KB
stdin
Standard input is empty
stdout
--------|  Incrementing ZonedDateTime  |-------------------
zdt.toString() 2015-10-24T10:00+01:00[Europe/London] | zdt.toInstant().toString(): 2015-10-24T09:00:00Z
zdt.toString() 2015-10-24T16:00+01:00[Europe/London] | zdt.toInstant().toString(): 2015-10-24T15:00:00Z
zdt.toString() 2015-10-24T22:00+01:00[Europe/London] | zdt.toInstant().toString(): 2015-10-24T21:00:00Z
zdt.toString() 2015-10-25T03:00Z[Europe/London] | zdt.toInstant().toString(): 2015-10-25T03:00:00Z
zdt.toString() 2015-10-25T09:00Z[Europe/London] | zdt.toInstant().toString(): 2015-10-25T09:00:00Z
zdt.toString() 2015-10-25T15:00Z[Europe/London] | zdt.toInstant().toString(): 2015-10-25T15:00:00Z
zdt.toString() 2015-10-25T21:00Z[Europe/London] | zdt.toInstant().toString(): 2015-10-25T21:00:00Z
zdt.toString() 2015-10-26T03:00Z[Europe/London] | zdt.toInstant().toString(): 2015-10-26T03:00:00Z
zdt.toString() 2015-10-26T09:00Z[Europe/London] | zdt.toInstant().toString(): 2015-10-26T09:00:00Z
zdt.toString() 2015-10-26T15:00Z[Europe/London] | zdt.toInstant().toString(): 2015-10-26T15:00:00Z

--------|  Incrementing Instant  |-------------------
>instant.toString() 2015-10-24T09:00:00Z | instant.atZone(z).toString(): 2015-10-24T10:00+01:00[Europe/London]
>instant.toString() 2015-10-24T15:00:00Z | instant.atZone(z).toString(): 2015-10-24T16:00+01:00[Europe/London]
>instant.toString() 2015-10-24T21:00:00Z | instant.atZone(z).toString(): 2015-10-24T22:00+01:00[Europe/London]
>instant.toString() 2015-10-25T03:00:00Z | instant.atZone(z).toString(): 2015-10-25T03:00Z[Europe/London]
>instant.toString() 2015-10-25T09:00:00Z | instant.atZone(z).toString(): 2015-10-25T09:00Z[Europe/London]
>instant.toString() 2015-10-25T15:00:00Z | instant.atZone(z).toString(): 2015-10-25T15:00Z[Europe/London]
>instant.toString() 2015-10-25T21:00:00Z | instant.atZone(z).toString(): 2015-10-25T21:00Z[Europe/London]
>instant.toString() 2015-10-26T03:00:00Z | instant.atZone(z).toString(): 2015-10-26T03:00Z[Europe/London]
>instant.toString() 2015-10-26T09:00:00Z | instant.atZone(z).toString(): 2015-10-26T09:00Z[Europe/London]
>instant.toString() 2015-10-26T15:00:00Z | instant.atZone(z).toString(): 2015-10-26T15:00Z[Europe/London]