fork(8) download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6. import java.time.*;
  7. import java.time.format.*;
  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. String input = "30 11 2012 12:08:56.235 +0700";
  15. DateTimeFormatter f = DateTimeFormatter.ofPattern ( "dd MM uuuu HH:mm:ss.SSS X" , Locale.US );
  16. OffsetDateTime odt = OffsetDateTime.parse ( input , f );
  17. System.out.println( "input: " + input );
  18. System.out.println( "odt: " + odt );
  19.  
  20. Instant instant = odt.toInstant();
  21. System.out.println( "instant: " + instant );
  22.  
  23. ZonedDateTime zdtKolkata = odt.toInstant ().atZone ( ZoneId.of ( "Asia/Kolkata" ) );
  24. System.out.println( "zdtKolkata: " + zdtKolkata );
  25.  
  26. // CAUTION: Not recommended. Avoid the legacy date-time classes such as `java.util.Date`.
  27. // CAUTION: Data-loss as `Instant` has resolution in nanoseconds but only milliseconds in `java.util.Date`.
  28. java.util.Date utilDate = java.util.Date.from( instant );
  29. System.out.println( "utilDate: " + utilDate );
  30. }
  31. }
Success #stdin #stdout 0.12s 712192KB
stdin
Standard input is empty
stdout
input: 30 11 2012 12:08:56.235 +0700
odt: 2012-11-30T12:08:56.235+07:00
instant: 2012-11-30T05:08:56.235Z
zdtKolkata: 2012-11-30T10:38:56.235+05:30[Asia/Kolkata]
utilDate: Fri Nov 30 05:08:56 GMT 2012