fork(3) 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.  
  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. String input = "2017-01-23 12:34 PM";
  17. DateTimeFormatter f = DateTimeFormatter.ofPattern ( "uuuu-MM-dd hh:mm a" );
  18. LocalDateTime ldt = LocalDateTime.parse ( input , f );
  19.  
  20. ZoneId z = ZoneId.of ( "America/Montreal" );
  21. ZonedDateTime zdt = ldt.atZone ( z );
  22.  
  23. Instant instant = zdt.toInstant ();
  24.  
  25. String output = instant.toString ()
  26. .replace ( "T" , " " ) // Substitute SPACE for T.
  27. .replace ( "Z" , " Z" ); // Insert SPACE before Z.
  28.  
  29. System.out.println ( "ldt.toString(): " + ldt );
  30. System.out.println ( "zdt.toString(): " + zdt );
  31. System.out.println ( "instant.toString(): " + instant );
  32. System.out.println ( "output: " + output );
  33.  
  34. }
  35. }
Success #stdin #stdout 0.13s 712192KB
stdin
Standard input is empty
stdout
ldt.toString(): 2017-01-23T12:34
zdt.toString(): 2017-01-23T12:34-05:00[America/Montreal]
instant.toString(): 2017-01-23T17:34:00Z
output: 2017-01-23 17:34:00 Z