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.  
  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 phoneCallStartedUtc = Instant.now() ; // A moment as seen in UTC.
  17. ZonedDateTime phoneCallStartedCasablanca = phoneCallStartedUtc.atZone( ZoneId.of( "Africa/Casablanca" ) ) ;
  18. ZonedDateTime phoneCallStartedTokyo = phoneCallStartedUtc.atZone( ZoneId.of( "Asia/Tokyo" ) ) ;
  19.  
  20. System.out.println( "phoneCallStartedUtc.toString(): " + phoneCallStartedUtc ) ;
  21. System.out.println( "phoneCallStartedCasablanca.toString(): " + phoneCallStartedCasablanca ) ;
  22. System.out.println( "phoneCallStartedTokyo.toString(): " + phoneCallStartedTokyo ) ;
  23.  
  24. // Store these three values.
  25. ZoneId zoneIdRome = ZoneId.of( "Europe/Rome" ) ;
  26. LocalTime restaurantOpening = LocalTime.of( 9 , 30 ) ; // 09:30 AM.
  27. LocalDate nextTuesday = LocalDate.now( zoneIdRome ).with( TemporalAdjusters.next( DayOfWeek.TUESDAY ) ) ;
  28.  
  29. // Do NOT store this next value. Use this next value only on-the-fly at runtime.
  30. ZonedDateTime momentWhenRestaurantIsExpectedToOpenNextTuesday =
  31. ZonedDateTime.of( nextTuesday , restaurantOpening , zoneIdRome ) ;
  32.  
  33. System.out.println( "restaurantOpening.toString(): " + restaurantOpening ) ;
  34. System.out.println( "nextTuesday.toString(): " + nextTuesday ) ;
  35. System.out.println( "momentWhenRestaurantIsExpectedToOpenNextTuesday.toString(): " + momentWhenRestaurantIsExpectedToOpenNextTuesday ) ;
  36.  
  37. }
  38. }
Success #stdin #stdout 0.18s 38604KB
stdin
Standard input is empty
stdout
phoneCallStartedUtc.toString(): 2021-03-28T21:52:15.959896Z
phoneCallStartedCasablanca.toString(): 2021-03-28T22:52:15.959896+01:00[Africa/Casablanca]
phoneCallStartedTokyo.toString(): 2021-03-29T06:52:15.959896+09:00[Asia/Tokyo]
restaurantOpening.toString(): 09:30
nextTuesday.toString(): 2021-03-30
momentWhenRestaurantIsExpectedToOpenNextTuesday.toString(): 2021-03-30T09:30+02:00[Europe/Rome]