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. import java.time.temporal.*;
  9. import java.time.format.*;
  10.  
  11.  
  12. /* Name of the class has to be "Main" only if the class is public. */
  13. class Ideone
  14. {
  15. public static void main (String[] args) throws java.lang.Exception
  16. {
  17.  
  18. // For `Europe/Amsterdam`, when local standard time was about to reach
  19. // Sunday, March 26, 2017, 2:00:00 am clocks were turned forward 1 hour to
  20. // Sunday, March 26, 2017, 3:00:00 am local daylight time instead.
  21.  
  22. LocalDate march26 = LocalDate.of ( 2017, Month.MARCH, 26 );
  23. LocalTime twoAm = LocalTime.of ( 2, 0 );
  24. ZoneId z = ZoneId.of ( "Europe/Amsterdam" );
  25. ZonedDateTime start = march26.atStartOfDay ( z );
  26. ZonedDateTime stop = ZonedDateTime.of ( march26, twoAm, z );
  27. long minutes = ChronoUnit.MINUTES.between ( start, stop );
  28. Duration duration = Duration.between ( start, stop );
  29. long durationAsMinutes = duration.toMinutes ( );
  30. int minuteOfDay = stop.get ( ChronoField.MINUTE_OF_DAY );
  31.  
  32. System.out.println ( "start: " + start );
  33. System.out.println ( "stop: " + stop );
  34. System.out.println ( "minutes: " + minutes );
  35. System.out.println ( "FYI: 4 * 60 = " + ( 4 * 60 ) + " | 3 * 60 = " + ( 3 * 60 ) + " | 2 * 60 = " + ( 2 * 60 ) );
  36. System.out.println ( "duration.toString(): " + duration + " | durationAsMinutes: " + durationAsMinutes );
  37. System.out.println ( "minuteOfDay: " + minuteOfDay );
  38.  
  39. }
  40. }
Success #stdin #stdout 0.15s 4386816KB
stdin
Standard input is empty
stdout
start: 2017-03-26T00:00+01:00[Europe/Amsterdam]
stop: 2017-03-26T03:00+02:00[Europe/Amsterdam]
minutes: 120
FYI: 4 * 60 = 240 | 3 * 60 = 180 | 2 * 60 = 120
duration.toString(): PT2H | durationAsMinutes: 120
minuteOfDay: 180