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.format.* ;
  9. import java.time.temporal.* ;
  10. import java.time.chrono.* ;
  11. import java.time.zone.* ;
  12. import java.sql.Timestamp ;
  13.  
  14.  
  15. /* Name of the class has to be "Main" only if the class is public. */
  16. class Ideone
  17. {
  18. public static void main (String[] args) throws java.lang.Exception
  19. {
  20.  
  21. LocalDate ld = LocalDate.of( 2020 , Month.JANUARY , 23 );
  22. LocalTime lt = LocalTime.of( 23 , 0 );
  23. ZoneId zMontreal = ZoneId.of( "America/Montreal" );
  24. ZonedDateTime zdt = ZonedDateTime.of( ld , lt , zMontreal );
  25. Instant instant = zdt.toInstant();
  26. // Convert from modern class to troubled legacy class `Timestamp`.
  27. java.sql.Timestamp ts = Timestamp.from( instant );
  28.  
  29. // Dump to console
  30. System.out.println( "zdt.toString() = " + zdt );
  31. System.out.println( "instant.toString() = " + instant );
  32. System.out.println( "ts.toString() = " + ts );
  33. System.out.println( "JVM’s current default time zone: " + ZoneId.systemDefault() );
  34.  
  35. // Imagine at runtime the JVM’s current default time zone is `Asia/Tokyo`.
  36. TimeZone.setDefault( TimeZone.getTimeZone( "Asia/Tokyo" ) );
  37. LocalDate localDate = ts.toLocalDateTime().toLocalDate();
  38.  
  39. // Compare results.
  40. boolean sameDate = ld.isEqual( localDate );
  41. System.out.println( "sameDate = " + sameDate + " | ld: " + ld + " localDate: " + localDate );
  42.  
  43. }
  44. }
Success #stdin #stdout 0.29s 41040KB
stdin
Standard input is empty
stdout
zdt.toString() = 2020-01-23T23:00-05:00[America/Montreal]
instant.toString() = 2020-01-24T04:00:00Z
ts.toString() = 2020-01-24 04:00:00.0
JVM’s current default time zone: GMT
sameDate = false | ld: 2020-01-23 localDate: 2020-01-24