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. // Do not use `Temporal`. Use the concrete classes, per design of java.time.
  16.  
  17. // Represent the person’s birth-moment.
  18. LocalDate ld = LocalDate.of( 2000 , Month.JANUARY , 23 ) ;
  19. LocalTime lt = LocalTime.of( 3 , 45 ) ;
  20. ZoneId z = ZoneId.of( "Pacific/Auckland" ) ;
  21. ZonedDateTime zdt = ZonedDateTime.of( ld , lt , z ) ;
  22.  
  23. // Adjust to UTC, if you care to.
  24. Instant instant = zdt.toInstant() ;
  25.  
  26. // Define the billions seconds as a `Duration`, a span of time unattached to the timeline.
  27. Duration d = Duration.ofSeconds( 1_000_000_000L ) ;
  28.  
  29. // Addition.
  30. ZonedDateTime zdtLater = zdt.plus( d ) ;
  31.  
  32. // Or, in UTC. Same moment as the `ZonedDateTime` but different wall-clock time.
  33. Instant instantLater = instant.plus( d ) ;
  34.  
  35. System.out.println( "zdt.toString(): " + zdt ) ;
  36. System.out.println( "instant.toString(): " + instant ) ;
  37. System.out.println( "d.toString(): " + d ) ;
  38. System.out.println( "zdtLater.toString(): " + zdtLater ) ;
  39. System.out.println( "instantLater.toString(): " + instantLater ) ;
  40.  
  41. }
  42. }
Success #stdin #stdout 0.25s 35568KB
stdin
Standard input is empty
stdout
zdt.toString(): 2000-01-23T03:45+13:00[Pacific/Auckland]
instant.toString(): 2000-01-22T14:45:00Z
d.toString(): PT277777H46M40S
zdtLater.toString(): 2031-10-01T05:31:40+13:00[Pacific/Auckland]
instantLater.toString(): 2031-09-30T16:31:40Z