fork(6) 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.ChronoUnit;
  9.  
  10. /* Name of the class has to be "Main" only if the class is public. */
  11. class Ideone
  12. {
  13. public static void main (String[] args) throws java.lang.Exception
  14. {
  15.  
  16. Instant start = OffsetDateTime.of( 2020 , 1 , 23 , 15 , 30 , 0 , 0 , ZoneOffset.UTC ).toInstant();
  17. Instant stop = OffsetDateTime.of( 2020 , 1 , 23 , 15 , 30 , 0 , 0 , ZoneOffset.UTC ).plusWeeks( 7 ).toInstant();
  18.  
  19. ZoneId z = ZoneId.of( "America/Montreal" );
  20.  
  21. ZonedDateTime startZdt = start.atZone( z );
  22. ZonedDateTime stopZdt = stop.atZone( z );
  23.  
  24. long weeksCount = ChronoUnit.WEEKS.between( startZdt , stopZdt );
  25.  
  26. System.out.println( "start.toString() = " + start );
  27. System.out.println( "stop.toString() = " + stop );
  28. System.out.println( "startZdt.toString() = " + startZdt );
  29. System.out.println( "stopZdt.toString() = " + stopZdt );
  30. System.out.println( "weeksCount: " + weeksCount );
  31.  
  32. System.out.println(
  33.  
  34. ChronoUnit
  35. .WEEKS
  36. .between(
  37. java.util.Date.from( start ).toInstant().atZone( ZoneId.of( "Asia/Tokyo" ) ) ,
  38. java.util.Date.from( stop ).toInstant().atZone( ZoneId.of( "Asia/Tokyo" ) )
  39. )
  40.  
  41. );
  42.  
  43. }
  44. }
Success #stdin #stdout 0.21s 38628KB
stdin
Standard input is empty
stdout
start.toString() = 2020-01-23T15:30:00Z
stop.toString() = 2020-03-12T15:30:00Z
startZdt.toString() = 2020-01-23T10:30-05:00[America/Montreal]
stopZdt.toString() = 2020-03-12T11:30-04:00[America/Montreal]
weeksCount: 7
7