fork 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.temporal.* ;
  9. import java.time.format.* ;
  10.  
  11. /* Name of the class has to be "Main" only if the class is public. */
  12. class Ideone
  13. {
  14. public static void main (String[] args) throws java.lang.Exception
  15. {
  16.  
  17. ZoneId z = ZoneId.of( "America/Montreal" ) ;
  18. LocalDate today = LocalDate.now( z ) ;
  19.  
  20. DateTimeFormatter f = DateTimeFormatter.ofPattern( "EEEE, MMM d uuuu" , Locale.US ) ;
  21. String input = "Monday, May 11 2015" ;
  22. LocalDate ld = LocalDate.parse( input , f ) ;
  23.  
  24. Period p = Period.between( ld , today ) ;
  25.  
  26. System.out.println( "today.toString(): " + today ) ;
  27. System.out.println( "ld.toString(): " + ld ) ;
  28. System.out.println( "p.toString(): " + p ) ;
  29.  
  30. String output =
  31. Period
  32. .between(
  33. LocalDate.parse(
  34. "Monday, May 11 2015" ,
  35. DateTimeFormatter.ofPattern( "EEEE, MMM d uuuu" , Locale.US )
  36. ) ,
  37. LocalDate.now( ZoneId.of( "America/Los_Angeles" ) )
  38. )
  39. .toString() ;
  40.  
  41. System.out.println( "output: " + output ) ;
  42.  
  43. }
  44. }
Success #stdin #stdout 0.2s 2184192KB
stdin
Standard input is empty
stdout
today.toString(): 2019-01-29
ld.toString(): 2015-05-11
p.toString(): P3Y8M18D
output: P3Y8M18D