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. LocalDate localDate = LocalDate.parse( "2016-12-28" );
  16. ZoneId zNewYork = ZoneId.of( "America/New_York" );
  17. ZonedDateTime zdtNewYork = localDate.atStartOfDay( zNewYork ); // First moment of the day in that zone on that date.
  18.  
  19. Instant instant = zdtNewYork.toInstant();
  20.  
  21. System.out.println( "localDate.toString(): " + localDate );
  22. System.out.println( "zdtNewYork.toString(): " + zdtNewYork );
  23. System.out.println( "instant.toString(): " + instant );
  24. System.out.println( "-------------------------------" );
  25.  
  26. List < ZoneId > hits = new ArrayList <>();
  27. LocalDate originalLocalDate = null;
  28. Set < String > zoneIds = ZoneId.getAvailableZoneIds(); // Gets the set of available zone IDs.
  29. for ( String zoneId : zoneIds )
  30. {
  31. ZoneId z = ZoneId.of( zoneId ); // Get zone with that name.
  32. ZonedDateTime zdt = instant.atZone( z );
  33. LocalDate ld = zdt.toLocalDate(); // Extract the date-only value, dropping the time-of-day and dropping the time zone.
  34. ZonedDateTime startOfDay = ld.atStartOfDay( z ); // Determine first moment of the day on that date in that zone.
  35. Instant instantOfStartOfDay = startOfDay.toInstant(); // Adjust back to UTC.
  36. boolean hit = instant.equals( instantOfStartOfDay );
  37. if ( hit )
  38. {
  39. originalLocalDate = ld;
  40. hits.add( z ); // Collect this time zone as the zone possibly used originally.
  41. }
  42. }
  43.  
  44. System.out.println( "originalLocalDate.toString(): " + originalLocalDate );
  45. System.out.println( "hits.toString(): " + hits );
  46.  
  47. }
  48. }
Success #stdin #stdout 0.54s 48696KB
stdin
Standard input is empty
stdout
localDate.toString(): 2016-12-28
zdtNewYork.toString(): 2016-12-28T00:00-05:00[America/New_York]
instant.toString(): 2016-12-28T05:00:00Z
-------------------------------
originalLocalDate.toString(): 2016-12-28
hits.toString(): [America/Panama, America/Indiana/Petersburg, America/Eirunepe, Cuba, Etc/GMT+5, Pacific/Easter, America/Fort_Wayne, America/Havana, America/Porto_Acre, US/Michigan, America/Louisville, America/Guayaquil, America/Indiana/Vevay, America/Indiana/Vincennes, America/Indianapolis, America/Iqaluit, America/Kentucky/Louisville, EST5EDT, America/Nassau, America/Jamaica, America/Atikokan, America/Kentucky/Monticello, America/Coral_Harbour, America/Cayman, Chile/EasterIsland, America/Indiana/Indianapolis, America/Thunder_Bay, America/Indiana/Marengo, America/Bogota, SystemV/EST5, US/Eastern, Canada/Eastern, America/Port-au-Prince, America/Nipigon, Brazil/Acre, US/East-Indiana, America/Cancun, America/Lima, America/Rio_Branco, America/Detroit, Jamaica, America/Pangnirtung, America/Montreal, America/Indiana/Winamac, America/New_York, America/Toronto, SystemV/EST5EDT]