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.  
  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. // Process input.
  17. String input = "08-27-2017 15:00:00" ;
  18. DateTimeFormatter f = DateTimeFormatter.ofPattern( "MM-dd-uuuu HH:mm:ss" ) ;
  19. LocalDateTime ldt = LocalDateTime.parse( input , f ) ;
  20.  
  21. // Apply an offset or time zone if you are certain that was intended for the input string.
  22. OffsetDateTime odt = ldt.atOffset( ZoneOffset.UTC ) ;
  23.  
  24. // Calculate delta from now.
  25. OffsetDateTime now = OffsetDateTime.now( ZoneOffset.UTC ) ;
  26. Duration d = Duration.between( odt , now ) ;
  27.  
  28. // Dump to console.
  29. System.out.println( "input: " + input ) ;
  30. System.out.println( "ldt.toString(): " + ldt ) ;
  31. System.out.println( "odt.toString(): " + odt ) ;
  32. System.out.println( "now.toString(): " + now ) ;
  33. System.out.println( "d.toString(): " + d ) ;
  34.  
  35.  
  36. }
  37. }
Success #stdin #stdout 0.14s 4386816KB
stdin
Standard input is empty
stdout
input: 08-27-2017 15:00:00
ldt.toString(): 2017-08-27T15:00
odt.toString(): 2017-08-27T15:00Z
now.toString(): 2017-08-27T21:21:32.374Z
d.toString(): PT6H21M32.374S